########################################################
## PROGRAM NAME: 006_pt.R                             ##
## AUTHOR: MATT MLECZKO                               ##
## DATE CREATED: 04/28/2021                           ##
## INPUTS:                                            ##
##    002_af_wide_final.Rda                           ##
##                                                    ##
## OUTPUTS:                                           ##
##                                                    ## 
## PURPOSE: Create transition matrices to capture     ##
##          stability/persistence of NH diversity;    ##
##          calculate values for Table 3 and          ##
##          Appendix Tables A3-A8                     ##
##                                                    ##
## LIST OF UPDATES:                                   ##
##                                                    ##
########################################################

log <- file("006_pt.txt") 
sink(log, append=TRUE)
sink(log, append=TRUE, type="message")

set.seed(08540)

## load libraries ##

library(tidycensus)
library(tidyverse)
library(dplyr)
library(foreign)
library(stringr)
library(tm)
library(gdata)
library(gsubfn)
library(readxl)
library(stargazer)
library(ggplot2)
library(haven)

## define paths ##
data_path <- "PATH TO DATA HERE"
progs <- "PATH TO PROGRAMS HERE"

## set working directory ##
setwd(data_path)

##
## load necessary functions ## 
##

`%notin%` <- Negate(`%in%`)
source(paste0(progs,"stata_merge.R"))

## load the analytic file ##
load("002_af_wide_final.Rda")

####################
####################
## JOINT DYNAMICS ##
####################
####################

##############################
## create transition matrix ## 
##############################

#################
## 2000 - 2010 ## 
#################

## pt 1 ## 

s1 <- table(low.diverse = factor(af.wide.final$ld_jt_2010[af.wide.final$ld_jt_2000==1]))[2]
  
  
s2 <- table(mod.diverse = factor(af.wide.final$md_jt_2010[af.wide.final$ld_jt_2000==1]))[2]
  
  
s3 <- table(factor(af.wide.final$hd_jt_2010[af.wide.final$ld_jt_2000==1],
                     levels = unique(af.wide.final$hd_jt_2010)))[2]
  
pt1.temp <- c(s1,s2,s3)
  

## pt 2 ## 

s1 <- table(low.diverse = factor(af.wide.final$ld_jt_2010[af.wide.final$md_jt_2000==1]))[2]

  
s2 <- table(mod.diverse = factor(af.wide.final$md_jt_2010[af.wide.final$md_jt_2000==1]))[2]
  
  
s3 <- table(factor(af.wide.final$hd_jt_2010[af.wide.final$md_jt_2000==1],
                     levels = unique(af.wide.final$hd_jt_2010)))[2]
  
pt2.temp <- c(s1,s2,s3)
  

## pt 3 ## 

s1.3 <- table(factor(af.wide.final$ld_jt_2010[af.wide.final$hd_jt_2000==1]))[2]


s2.3 <- table(factor(af.wide.final$md_jt_2010[af.wide.final$hd_jt_2000==1]))[2]

s3.3 <- table(af.wide.final$hd_jt_2010[af.wide.final$hd_jt_2000 ==1])[2]

pt3.temp <- c(s1.3,s2.3,s3.3)


## put it all together ## 

tmatrix.2000.2010.jt <- as.data.frame(rbind(pt1.temp, 
                                      pt2.temp,
                                      pt3.temp))

## create total columns ## 

tmatrix.2000.2010.jt$Total <- rowSums(tmatrix.2000.2010.jt, na.rm=T)

row.total <- colSums(tmatrix.2000.2010.jt, na.rm=T)

## final transition matrix ## 

tmatrix.2000.2010.final.jt <- rbind(tmatrix.2000.2010.jt,
                                    row.total)

row.names(tmatrix.2000.2010.final.jt) <- c("Low diversity",
                                           "Moderate diversity",
                                           "High diversity",
                                           "Total")

names(tmatrix.2000.2010.final.jt) <- c("Low diversity",
                                       "Moderate diversity",
                                       "High diversity",
                                       "Total")
##
## display shares ## 
## this is the 2000-2010 portion of table 3 ##
##

tmatrix.2000.2010.final.jt/tmatrix.2000.2010.final.jt$Total

## output table ## 
stargazer(tmatrix.2000.2010.final.jt,
          title = "Neighborhood transition matrix in all MSAs, 2000-2010",
          summary= c(rep(FALSE, 10)),
          rownames = T)

#################
## 2010 - 2020 ## 
#################

## pt 1 ## 

s1 <- table(low.diverse = factor(af.wide.final$ld_jt_2020[af.wide.final$ld_jt_2010==1]))[2]


s2 <- table(mod.diverse = factor(af.wide.final$md_jt_2020[af.wide.final$ld_jt_2010==1]))[2]


s3 <- table(factor(af.wide.final$hd_jt_2020[af.wide.final$ld_jt_2010==1],
                   levels = unique(af.wide.final$hd_jt_2020)))[2]

pt1.temp <- c(s1,s2,s3)


## pt 2 ## 

s1 <- table(low.diverse = factor(af.wide.final$ld_jt_2020[af.wide.final$md_jt_2010==1]))[2]


s2 <- table(mod.diverse = factor(af.wide.final$md_jt_2020[af.wide.final$md_jt_2010==1]))[2]


s3 <- table(factor(af.wide.final$hd_jt_2020[af.wide.final$md_jt_2010==1],
                   levels = unique(af.wide.final$hd_jt_2020)))[2]

pt2.temp <- c(s1,s2,s3)


## pt 3 ## 

s1.3 <- table(factor(af.wide.final$ld_jt_2020[af.wide.final$hd_jt_2010==1]))[2]


s2.3 <- table(factor(af.wide.final$md_jt_2020[af.wide.final$hd_jt_2010==1]))[2]

s3.3 <- table(af.wide.final$hd_jt_2020[af.wide.final$hd_jt_2010 ==1])[2]

pt3.temp <- c(s1.3,s2.3,s3.3)


## put it all together ## 

tmatrix.2010.2020.jt <- as.data.frame(rbind(pt1.temp, 
                                            pt2.temp,
                                            pt3.temp))

## create total columns ## 

tmatrix.2010.2020.jt$Total <- rowSums(tmatrix.2010.2020.jt, na.rm=T)

row.total <- colSums(tmatrix.2010.2020.jt, na.rm=T)

## final transition matrix ## 

tmatrix.2010.2020.final.jt <- rbind(tmatrix.2010.2020.jt,
                                    row.total)

row.names(tmatrix.2010.2020.final.jt) <- c("Low diversity",
                                           "Moderate diversity",
                                           "High diversity",
                                           "Total")

names(tmatrix.2010.2020.final.jt) <- c("Low diversity",
                                       "Moderate diversity",
                                       "High diversity",
                                       "Total")
##
## display shares ## 
## this is the 2010-2020 portion of table 3 ##
##

tmatrix.2010.2020.final.jt/tmatrix.2010.2020.final.jt$Total

##
## figure ##
##

tdata.jt <- data.frame(num = c(tmatrix.2000.2010.final.jt$`Low diversity`[4],
                               tmatrix.2000.2010.final.jt$`Moderate diversity`[4],
                               tmatrix.2000.2010.final.jt$`High diversity`[4],
                               tmatrix.2000.2010.final.jt$`Total`[1],
                               tmatrix.2000.2010.final.jt$`Total`[2],
                               tmatrix.2000.2010.final.jt$`Total`[3],
                               tmatrix.2010.2020.final.jt$`Total`[1],
                               tmatrix.2010.2020.final.jt$`Total`[2],
                               tmatrix.2010.2020.final.jt$`Total`[3]),
                       year = c(2000,
                                2000,
                                2000,
                                2010,
                                2010,
                                2010,
                                2020,
                                2020,
                                2020),
                       cat = c("Low diversity",
                               "Moderate diversity",
                               "High diversity",
                               "Low diversity",
                               "Moderate diversity",
                               "High diversity",
                               "Low diversity",
                               "Moderate diversity",
                               "High diversity"),
                       prop = c(round(prop.table(table(af.wide.final$ld_jt_2000))[2],2),
                                round(prop.table(table(af.wide.final$md_jt_2000))[2],2),
                                round(prop.table(table(af.wide.final$hd_jt_2000))[2],2),
                                round(prop.table(table(af.wide.final$ld_jt_2010))[2],2),
                                round(prop.table(table(af.wide.final$md_jt_2010))[2],2),
                                round(prop.table(table(af.wide.final$hd_jt_2000))[2],2),
                                round(prop.table(table(af.wide.final$ld_jt_2020))[2],2),
                                round(prop.table(table(af.wide.final$md_jt_2020))[2],2),
                                round(prop.table(table(af.wide.final$hd_jt_2020))[2],2)),
                       pct_lab = c(paste0(round(prop.table(table(af.wide.final$ld_jt_2000))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$md_jt_2000))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$hd_jt_2000))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$ld_jt_2010))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$md_jt_2010))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$hd_jt_2000))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$ld_jt_2020))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$md_jt_2020))[2],4)*100,"%"),
                               paste0(round(prop.table(table(af.wide.final$hd_jt_2020))[2],4)*100,"%")))

tdata.jt$pct <- tdata.jt$prop * 100

jt.fig <- ggplot(tdata.jt, 
             aes(y=prop, x=year,
                 fill=factor(cat, levels = c("Low diversity",
                                             "Moderate diversity",
                                             "High diversity")))) + 
  geom_bar(position="fill", stat="identity") + 
  geom_text(aes(label = prop), 
            position = position_stack(vjust = 0.5), size = 3) +
  xlab("Year") + 
  ylab("Share") +
  labs(fill = "Neighborhood") + 
  labs(title = "")

jt.fig

##########################
##########################
## ETHNORACIAL DYNAMICS ##
##########################
##########################

##############################
## create transition matrix ## 
##############################

#################
## 2000 - 2010 ## 
#################

## pt 1 ## 

input.vars.2000 <- c("per_aian", 
                     "per_asian", 
                     "per_black", 
                     "per_hl", 
                     "per_other", 
                     "per_white")

low.diverse.rows <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_er_2010[af.wide.final$ld_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                        levels = unique(af.wide.final$ld_er_2010)),
                   maj.race = factor(af.wide.final$maj_race_2010[af.wide.final$ld_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                     levels = unique(af.wide.final$maj_race_2010)))["1",]
  
  s1 <- c(s1.temp["per_aian"],
          s1.temp["per_asian"],
          s1.temp["per_black"],
          s1.temp["per_hl"],
          s1.temp["per_other"],
          s1.temp["per_white"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_er_2010[af.wide.final$ld_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                        levels = unique(af.wide.final$md_er_2010)),
                   maj.race = factor(af.wide.final$maj_race_2010[af.wide.final$ld_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                     levels = unique(af.wide.final$maj_race_2010)))["1",]
  
  s2 <- c(s2.temp["per_aian"],
          s2.temp["per_asian"],
          s2.temp["per_black"],
          s2.temp["per_hl"],
          s2.temp["per_other"],
          s2.temp["per_white"])
  
  s3 <- table(factor(af.wide.final$hd_er_2010[af.wide.final$ld_er_2000==1 & af.wide.final$maj_race_2000 == x],
                     levels = unique(af.wide.final$hd_er_2010)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:13, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt1 <- lapply(input.vars.2000, low.diverse.rows)

pt1.final <- bind_rows(pt1)

row.names(pt1.final) <- c("AIAN, low diversity",
                          "Asian, low diversity",
                          "Black, low diversity",
                          "Hispanic/Latino, low diversity",
                          "Other, low diversity",
                          "White, low diversity")

names(pt1.final) <- c("AIAN, low diversity",
                      "Asian, low diversity",
                      "Black, low diversity",
                      "Hispanic/Latino, low diversity",
                      "Other, low diversity",
                      "White, low diversity",
                      "AIAN, moderate diversity",
                      "Asian, moderate diversity",
                      "Black, moderate diversity",
                      "Hispanic/Latino, moderate diversity",
                      "Other, moderate diversity",
                      "White, moderate diversity",
                      "High diversity")


## pt 2 ## 

mod.diverse.rows <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_er_2010[af.wide.final$md_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                        levels = unique(af.wide.final$ld_er_2010)),
                   maj.race = factor(af.wide.final$maj_race_2010[af.wide.final$md_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                     levels = unique(af.wide.final$maj_race_2010)))["1",]
  
  s1 <- c(s1.temp["per_aian"],
          s1.temp["per_asian"],
          s1.temp["per_black"],
          s1.temp["per_hl"],
          s1.temp["per_other"],
          s1.temp["per_white"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_er_2010[af.wide.final$md_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                        levels = unique(af.wide.final$md_er_2010)),
                   maj.race = factor(af.wide.final$maj_race_2010[af.wide.final$md_er_2000==1 & af.wide.final$maj_race_2000 == x],
                                     levels = unique(af.wide.final$maj_race_2010)))["1",]
  
  s2 <- c(s2.temp["per_aian"],
          s2.temp["per_asian"],
          s2.temp["per_black"],
          s2.temp["per_hl"],
          s2.temp["per_other"],
          s2.temp["per_white"])
  
  s3 <- table(factor(af.wide.final$hd_er_2010[af.wide.final$md_er_2000==1 & af.wide.final$maj_race_2000 == x],
                     levels = unique(af.wide.final$hd_er_2010)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:13, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt2 <- lapply(input.vars.2000, mod.diverse.rows)

pt2.final <- bind_rows(pt2)

row.names(pt2.final) <- c("AIAN, moderate diversity",
                          "Asian, moderate diversity",
                          "Black, moderate diversity",
                          "Hispanic/Latino, moderate diversity",
                          "Other, moderate diversity",
                          "White, moderate diversity")

names(pt2.final) <- c("AIAN, low diversity",
                      "Asian, low diversity",
                      "Black, low diversity",
                      "Hispanic/Latino, low diversity",
                      "Other, low diversity",
                      "White, low diversity",
                      "AIAN, moderate diversity",
                      "Asian, moderate diversity",
                      "Black, moderate diversity",
                      "Hispanic/Latino, moderate diversity",
                      "Other, moderate diversity",
                      "White, moderate diversity",
                      "High diversity")

## pt 3 ## 

s1.3.temp <- table(factor(af.wide.final$ld_er_2010[af.wide.final$hd_er_2000==1],
                          levels = unique(af.wide.final$ld_er_2010)),
                   factor(af.wide.final$maj_race_2010[af.wide.final$hd_er_2000==1],
                          levels = unique(af.wide.final$maj_race_2010)))["1",]

s1.3 <- c(s1.3.temp["per_aian"],
          s1.3.temp["per_asian"],
          s1.3.temp["per_black"],
          s1.3.temp["per_hl"],
          s1.3.temp["per_other"],
          s1.3.temp["per_white"])

s2.3.temp <- table(factor(af.wide.final$md_er_2010[af.wide.final$hd_er_2000==1],
                          levels = unique(af.wide.final$md_er_2010)),
                   factor(af.wide.final$maj_race_2010[af.wide.final$hd_er_2000==1],
                          levels = unique(af.wide.final$maj_race_2010)))["1",]

s2.3 <- c(s2.3.temp["per_aian"],
          s2.3.temp["per_asian"],
          s2.3.temp["per_black"],
          s2.3.temp["per_hl"],
          s2.3.temp["per_other"],
          s2.3.temp["per_white"])

s3.3 <- table(af.wide.final$hd_er_2010[af.wide.final$hd_er_2000 ==1])[2]

pt3 <- c(s1.3,s2.3,s3.3)

pt3.final <- cbind.data.frame(split(pt3, rep(1:13, times=1)),
                              stringsAsFactors=F)

row.names(pt3.final) <- c("High diversity")

names(pt3.final) <- c("AIAN, low diversity",
                      "Asian, low diversity",
                      "Black, low diversity",
                      "Hispanic/Latino, low diversity",
                      "Other, low diversity",
                      "White, low diversity",
                      "AIAN, moderate diversity",
                      "Asian, moderate diversity",
                      "Black, moderate diversity",
                      "Hispanic/Latino, moderate diversity",
                      "Other, moderate diversity",
                      "White, moderate diversity",
                      "High diversity")

## put it all together ## 

tmatrix.2000.2010 <- rbind(pt1.final, 
                           pt2.final,
                           pt3.final)

## create total columns ## 

tmatrix.2000.2010$Total <- rowSums(tmatrix.2000.2010)

row.total <- colSums(tmatrix.2000.2010)

## final transition matrix ## 

tmatrix.2000.2010.final <- rbind(tmatrix.2000.2010,
                                 row.total)

row.names(tmatrix.2000.2010.final)[14] <- "Total"

## output table ## on
stargazer(tmatrix.2000.2010.final,
          title = "Neighborhood transition matrix in all MSAs, 2000-2010",
          summary= c(rep(FALSE, 10)),
          rownames = T)


#################
## 2010 - 2020 ##
#################

## pt 1 ## 

input.vars.2010 <- c("per_aian", 
                     "per_asian", 
                     "per_black", 
                     "per_hl", 
                     "per_other", 
                     "per_white")

low.diverse.rows.2010 <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_er_2020[af.wide.final$ld_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                        levels = unique(af.wide.final$ld_er_2020)),
                   maj.race = factor(af.wide.final$maj_race_2020[af.wide.final$ld_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                     levels = unique(af.wide.final$maj_race_2020)))["1",]
  
  s1 <- c(s1.temp["per_aian"],
          s1.temp["per_asian"],
          s1.temp["per_black"],
          s1.temp["per_hl"],
          s1.temp["per_other"],
          s1.temp["per_white"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_er_2020[af.wide.final$ld_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                        levels = unique(af.wide.final$md_er_2020)),
                   maj.race = factor(af.wide.final$maj_race_2020[af.wide.final$ld_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                     levels = unique(af.wide.final$maj_race_2020)))["1",]
  
  s2 <- c(s2.temp["per_aian"],
          s2.temp["per_asian"],
          s2.temp["per_black"],
          s2.temp["per_hl"],
          s2.temp["per_other"],
          s2.temp["per_white"])
  
  s3 <- table(factor(af.wide.final$hd_er_2020[af.wide.final$ld_er_2010==1 & af.wide.final$maj_race_2010 == x],
                     levels = unique(af.wide.final$hd_er_2020)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:13, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt1.2010 <- lapply(input.vars.2010, low.diverse.rows.2010)

pt1.final.2010 <- bind_rows(pt1.2010)

row.names(pt1.final.2010) <- c("AIAN, low diversity",
                               "Asian, low diversity",
                               "Black, low diversity",
                               "Hispanic/Latino, low diversity",
                               "Other, low diversity",
                               "White, low diversity")

names(pt1.final.2010) <- c("AIAN, low diversity",
                           "Asian, low diversity",
                           "Black, low diversity",
                           "Hispanic/Latino, low diversity",
                           "Other, low diversity",
                           "White, low diversity",
                           "AIAN, moderate diversity",
                           "Asian, moderate diversity",
                           "Black, moderate diversity",
                           "Hispanic/Latino, moderate diversity",
                           "Other, moderate diversity",
                           "White, moderate diversity",
                           "High diversity")

## pt 2 ## 

mod.diverse.rows.2010 <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_er_2020[af.wide.final$md_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                        levels = unique(af.wide.final$ld_er_2020)),
                   maj.race = factor(af.wide.final$maj_race_2020[af.wide.final$md_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                     levels = unique(af.wide.final$maj_race_2020)))["1",]
  
  s1 <- c(s1.temp["per_aian"],
          s1.temp["per_asian"],
          s1.temp["per_black"],
          s1.temp["per_hl"],
          s1.temp["per_other"],
          s1.temp["per_white"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_er_2020[af.wide.final$md_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                        levels = unique(af.wide.final$md_er_2020)),
                   maj.race = factor(af.wide.final$maj_race_2020[af.wide.final$md_er_2010==1 & af.wide.final$maj_race_2010 == x],
                                     levels = unique(af.wide.final$maj_race_2020)))["1",]
  
  s2 <- c(s2.temp["per_aian"],
          s2.temp["per_asian"],
          s2.temp["per_black"],
          s2.temp["per_hl"],
          s2.temp["per_other"],
          s2.temp["per_white"])
  
  s3 <- table(factor(af.wide.final$hd_er_2020[af.wide.final$md_er_2010==1 & af.wide.final$maj_race_2010 == x],
                     levels = unique(af.wide.final$hd_er_2020)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:13, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt2.2010 <- lapply(input.vars.2010, mod.diverse.rows.2010)

pt2.final.2010 <- bind_rows(pt2.2010)

row.names(pt2.final.2010) <- c("AIAN, moderate diversity",
                               "Asian, moderate diversity",
                               "Black, moderate diversity",
                               "Hispanic/Latino, moderate diversity",
                               "Other, moderate diversity",
                               "White, moderate diversity")

names(pt2.final.2010) <- c("AIAN, low diversity",
                           "Asian, low diversity",
                           "Black, low diversity",
                           "Hispanic/Latino, low diversity",
                           "Other, low diversity",
                           "White, low diversity",
                           "AIAN, moderate diversity",
                           "Asian, moderate diversity",
                           "Black, moderate diversity",
                           "Hispanic/Latino, moderate diversity",
                           "Other, moderate diversity",
                           "White, moderate diversity",
                           "High diversity")

## pt 3 ## 

s1.3.temp.2010 <- table(factor(af.wide.final$ld_er_2020[af.wide.final$hd_er_2010==1],
                               levels = unique(af.wide.final$ld_er_2020)),
                        factor(af.wide.final$maj_race_2020[af.wide.final$hd_er_2010==1],
                               levels = unique(af.wide.final$maj_race_2020)))["1",]

s1.3.2010 <- c(s1.3.temp.2010["per_aian"],
               s1.3.temp.2010["per_asian"],
               s1.3.temp.2010["per_black"],
               s1.3.temp.2010["per_hl"],
               s1.3.temp.2010["per_other"],
               s1.3.temp.2010["per_white"])

s2.3.temp.2010 <- table(factor(af.wide.final$md_er_2020[af.wide.final$hd_er_2010==1],
                               levels = unique(af.wide.final$md_er_2020)),
                        factor(af.wide.final$maj_race_2020[af.wide.final$hd_er_2010==1],
                               levels = unique(af.wide.final$maj_race_2020)))["1",]

s2.3.2010 <- c(s2.3.temp.2010["per_aian"],
               s2.3.temp.2010["per_asian"],
               s2.3.temp.2010["per_black"],
               s2.3.temp.2010["per_hl"],
               s2.3.temp.2010["per_other"],
               s2.3.temp.2010["per_white"])

s3.3.2010 <- table(af.wide.final$hd_er_2020[af.wide.final$hd_er_2010 ==1])[2]

pt3.2010 <- c(s1.3.2010,s2.3.2010,s3.3.2010)

pt3.final.2010 <- cbind.data.frame(split(pt3.2010, rep(1:13, times=1)),
                                   stringsAsFactors=F)

row.names(pt3.final.2010) <- c("High diversity")

names(pt3.final.2010) <- c("AIAN, low diversity",
                           "Asian, low diversity",
                           "Black, low diversity",
                           "Hispanic/Latino, low diversity",
                           "Other, low diversity",
                           "White, low diversity",
                           "AIAN, moderate diversity",
                           "Asian, moderate diversity",
                           "Black, moderate diversity",
                           "Hispanic/Latino, moderate diversity",
                           "Other, moderate diversity",
                           "White, moderate diversity",
                           "High diversity")

## put it all together ## 

tmatrix.2010.2020 <- rbind(pt1.final.2010, 
                           pt2.final.2010,
                           pt3.final.2010)

## create total columns ## 

tmatrix.2010.2020$Total <- rowSums(tmatrix.2010.2020)

row.total.2010 <- colSums(tmatrix.2010.2020)

## final transition matrix ## 

tmatrix.2010.2020.final <- rbind(tmatrix.2010.2020,
                                 row.total.2010)

row.names(tmatrix.2010.2020.final)[14] <- "Total"

## output table ## on
stargazer(tmatrix.2010.2020.final,
          title = "Neighborhood transition matrix in all MSAs, 2010-2020",
          summary= c(rep(FALSE, 10)),
          rownames = T)


##################
## create graph ##
##################

t2000 <- tmatrix.2000.2010.final$Total[1:14]
tdata2000 <- as.data.frame(cbind(t2000, 
                                 rep(2000,14)))

t2010 <- t(tmatrix.2000.2010.final[14,1:14])
rnames <- rownames(t2010)

t2010b <- tmatrix.2010.2020.final$Total[1:14]
tdata2010b <- as.data.frame(cbind(t2010, 
                                  rep(2000,14)))

t2020 <- t(tmatrix.2010.2020.final[14,1:14])

tdata2000$rnames <- rnames
colnames(tdata2000) <- c("num","year","cat")

tdata2000.final <- tdata2000 %>%
  filter(cat %notin% c("AIAN, low diversity",
                       "AIAN, moderate diversity",
                       "Other, low diversity",
                       "Other, moderate diversity",
                       "Total"))

tdata2000.final$pct <- tdata2000.final$num/sum(tdata2000.final$num)
sum(tdata2000.final$pct)

tdata2000.final$prop <- ifelse(tdata2000.final$pct >= .02, 
                               round(tdata2000.final$pct,2),"")

tdata2010 <- as.data.frame(cbind(t2010,
                                 rep(2010,14),
                                 rnames))

colnames(tdata2010) <- c("num","year","cat")

tdata2010.final <- tdata2010 %>%
  filter(cat %notin% c("AIAN, low diversity",
                       "AIAN, moderate diversity",
                       "Other, low diversity",
                       "Other, moderate diversity",
                       "Total")) %>%
  mutate(num2 = as.numeric(as.character(num))) %>%
  select(-num) %>%
  rename(num = num2)

tdata2010.final$pct <- tdata2010.final$num/sum(tdata2010.final$num)
sum(tdata2010.final$pct)

tdata2010.final$prop <- ifelse(tdata2010.final$pct >= .02, 
                               round(tdata2010.final$pct,2),"")

tdata2020 <- as.data.frame(cbind(t2020,
                                 rep(2020,14),
                                 rnames))

colnames(tdata2020) <- c("num","year","cat")

tdata2020.final <- tdata2020 %>%
  filter(cat %notin% c("AIAN, low diversity",
                       "AIAN, moderate diversity",
                       "Other, low diversity",
                       "Other, moderate diversity",
                       "Total")) %>%
  mutate(num2 = as.numeric(as.character(num))) %>%
  select(-num) %>%
  rename(num = num2)

tdata2020.final$pct <- tdata2020.final$num/sum(tdata2020.final$num)
sum(tdata2020.final$pct)

tdata2020.final$prop <- ifelse(tdata2020.final$pct >= .02, 
                               round(tdata2020.final$pct,2),"")

tdata <- rbind(tdata2000.final,
               tdata2010.final,
               tdata2020.final)

rownames(tdata) <- seq(1,27,by=1)

er.fig <- ggplot(tdata, 
             aes(y=pct, x=year,
                 fill=factor(cat, levels = c("Asian, low diversity",
                            "Asian, moderate diversity",
                            "Black, low diversity",
                            "Black, moderate diversity",
                            "Hispanic/Latino, low diversity",
                            "Hispanic/Latino, moderate diversity",
                            "White, low diversity",
                            "White, moderate diversity",
                            "High diversity")))) + 
  geom_bar(position="fill", stat="identity") + 
  scale_fill_manual(values=c("seagreen3",
                             "seagreen4",
                             "dodgerblue",
                             "royalblue1",
                             "orchid3",
                             "orchid4",
                             "coral2",
                             "coral3",
                             "gold2")) + 
  geom_text(aes(label = prop), 
            position = position_stack(vjust = 0.5), size = 3) +
  xlab("Year") + 
  ylab("Share") +
  labs(fill = "Neighborhood") + 
  labs(title = "") + 
  theme_minimal()

er.fig

## probability of transition 2000-2010 ##

table(af.wide.final$ld_er_2000, useNA = "ifany")
table(af.wide.final$md_er_2000, useNA = "ifany")

ptld0010.ld.er <- table(af.wide.final$ld_er_2000)[2]
ptld0010.md.er <- table(af.wide.final$md_er_2000)[2]

pt.ld.2000.2010 <- data.frame(ld.sgd = c(tmatrix.2000.2010.final$`Asian, low diversity`[2]/tmatrix.2000.2010.final$Total[2],
                                         tmatrix.2000.2010.final$`Black, low diversity`[3]/tmatrix.2000.2010.final$Total[3],
                                         tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[4]/tmatrix.2000.2010.final$Total[4],
                                         tmatrix.2000.2010.final$`White, low diversity`[6]/tmatrix.2000.2010.final$Total[6],
                                         sum(tmatrix.2000.2010.final$`aian, low diversity`[1] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[2] , 
                                             tmatrix.2000.2010.final$`Black, low diversity`[3] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[4] , 
                                             tmatrix.2000.2010.final$`Other, low diversity`[5] , 
                                             tmatrix.2000.2010.final$`White, low diversity`[6])/ptld0010.ld.er),
                           ld.ogd = c(sum(tmatrix.2000.2010.final$`aian, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[2])/tmatrix.2000.2010.final$Total[2],
                                      sum(tmatrix.2000.2010.final$`aian, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[3])/tmatrix.2000.2010.final$Total[3],
                                      sum(tmatrix.2000.2010.final$`aian, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[4])/tmatrix.2000.2010.final$Total[4],
                                      sum(tmatrix.2000.2010.final$`aian, low diversity`[6] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[6] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[6] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[6],
                                          tmatrix.2000.2010.final$`Other, low diversity`[6])/tmatrix.2000.2010.final$Total[6],
                                      sum(tmatrix.2000.2010.final$`Asian, low diversity`[1] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[1] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[1] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[1] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[1] ,
                                          tmatrix.2000.2010.final$`aian, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[2] ,
                                          tmatrix.2000.2010.final$`aian, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[3] ,
                                          tmatrix.2000.2010.final$`aian, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`Other, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[4] ,
                                          tmatrix.2000.2010.final$`aian, low diversity`[5] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[5] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[5] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[5] ,
                                          tmatrix.2000.2010.final$`White, low diversity`[5] ,
                                          tmatrix.2000.2010.final$`aian, low diversity`[6] ,
                                          tmatrix.2000.2010.final$`Asian, low diversity`[6] ,
                                          tmatrix.2000.2010.final$`Black, low diversity`[6] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[6],
                                          tmatrix.2000.2010.final$`Other, low diversity`[6])/ptld0010.ld.er),
                           md.sgd = c(tmatrix.2000.2010.final$`Asian, moderate diversity`[2]/tmatrix.2000.2010.final$Total[2],
                                      tmatrix.2000.2010.final$`Black, moderate diversity`[3]/tmatrix.2000.2010.final$Total[3],
                                      tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[4]/tmatrix.2000.2010.final$Total[4],
                                      tmatrix.2000.2010.final$`White, moderate diversity`[6]/tmatrix.2000.2010.final$Total[6],
                                      sum(tmatrix.2000.2010.final$`aian, moderate diversity`[1],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[3],
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[4],
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[5],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[6])/ptld0010.ld.er),
                           md.ogd = c(sum(tmatrix.2000.2010.final$`aian, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[2])/tmatrix.2000.2010.final$Total[2],
                                      sum(tmatrix.2000.2010.final$`aian, moderate diversity`[3],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[3] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[3] ,
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[3],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[3])/tmatrix.2000.2010.final$Total[3],
                                      sum(tmatrix.2000.2010.final$`aian, moderate diversity`[4],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[4] ,
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[4] ,
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[4],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[4])/tmatrix.2000.2010.final$Total[4],
                                      sum(tmatrix.2000.2010.final$`aian, moderate diversity`[6],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[6] ,
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[6] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[6],
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[6])/tmatrix.2000.2010.final$Total[6],
                                      sum(tmatrix.2000.2010.final$`Asian, moderate diversity`[1],
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[1] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[1] ,
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[1],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[1] ,
                                          tmatrix.2000.2010.final$`aian, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[2] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[2] ,
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[2],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[2] ,
                                          tmatrix.2000.2010.final$`aian, moderate diversity`[3],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[3] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[3] ,
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[3],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[3] ,
                                          tmatrix.2000.2010.final$`aian, moderate diversity`[4],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[4] ,
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[4] ,
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[4],
                                          tmatrix.2000.2010.final$`White, moderate diversity`[4] ,
                                          tmatrix.2000.2010.final$`aian, moderate diversity`[5],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[5],
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[5] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[5] ,
                                          tmatrix.2000.2010.final$`White, moderate diversity`[5] ,
                                          tmatrix.2000.2010.final$`aian, moderate diversity`[6],
                                          tmatrix.2000.2010.final$`Asian, moderate diversity`[6] ,
                                          tmatrix.2000.2010.final$`Black, moderate diversity`[6] ,
                                          tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[6],
                                          tmatrix.2000.2010.final$`Other, moderate diversity`[6])/ptld0010.ld.er),
                           hd = c(#tmatrix.2000.2010.final$`High diversity`[1]/tmatrix.2000.2010.final$Total[1],
                                  tmatrix.2000.2010.final$`High diversity`[2]/tmatrix.2000.2010.final$Total[2],
                                  tmatrix.2000.2010.final$`High diversity`[3]/tmatrix.2000.2010.final$Total[3],
                                  tmatrix.2000.2010.final$`High diversity`[4]/tmatrix.2000.2010.final$Total[4],
                                  #tmatrix.2000.2010.final$`High diversity`[5]/tmatrix.2000.2010.final$Total[5],
                                  tmatrix.2000.2010.final$`High diversity`[6]/tmatrix.2000.2010.final$Total[6],
                                  sum(tmatrix.2000.2010.final$`High diversity`[1]+
                                      tmatrix.2000.2010.final$`High diversity`[2] +
                                      tmatrix.2000.2010.final$`High diversity`[3]+ 
                                      tmatrix.2000.2010.final$`High diversity`[4]+
                                      tmatrix.2000.2010.final$`High diversity`[5]+
                                      tmatrix.2000.2010.final$`High diversity`[6])/ptld0010.ld.er))

pt.md.2000.2010 <- data.frame(ld.sgd = c(tmatrix.2000.2010.final$`Asian, low diversity`[8]/tmatrix.2000.2010.final$Total[8],
                                         tmatrix.2000.2010.final$`Black, low diversity`[9]/tmatrix.2000.2010.final$Total[9],
                                         tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[10]/tmatrix.2000.2010.final$Total[10],
                                         tmatrix.2000.2010.final$`White, low diversity`[12]/tmatrix.2000.2010.final$Total[12],
                                         sum(tmatrix.2000.2010.final$`aian, low diversity`[7] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[8] , 
                                             tmatrix.2000.2010.final$`Black, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[10] , 
                                             tmatrix.2000.2010.final$`Other, low diversity`[11] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[12])/ptld0010.md.er),
                              ld.ogd = c(sum(tmatrix.2000.2010.final$`aian, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[8])/tmatrix.2000.2010.final$Total[8],
                                         sum(tmatrix.2000.2010.final$`aian, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[9])/tmatrix.2000.2010.final$Total[9],
                                         sum(tmatrix.2000.2010.final$`aian, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[10])/tmatrix.2000.2010.final$Total[10],
                                         sum(tmatrix.2000.2010.final$`aian, low diversity`[12] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[12] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[12] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[12],
                                             tmatrix.2000.2010.final$`Other, low diversity`[12])/tmatrix.2000.2010.final$Total[12],
                                         sum(tmatrix.2000.2010.final$`Asian, low diversity`[7] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[7] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[7] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[7] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[7] ,
                                             tmatrix.2000.2010.final$`aian, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[8] ,
                                             tmatrix.2000.2010.final$`aian, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[9] ,
                                             tmatrix.2000.2010.final$`aian, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`Other, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[10] ,
                                             tmatrix.2000.2010.final$`aian, low diversity`[11] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[11] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[11] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[11] ,
                                             tmatrix.2000.2010.final$`White, low diversity`[11] ,
                                             tmatrix.2000.2010.final$`aian, low diversity`[12] ,
                                             tmatrix.2000.2010.final$`Asian, low diversity`[12] ,
                                             tmatrix.2000.2010.final$`Black, low diversity`[12] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[12],
                                             tmatrix.2000.2010.final$`Other, low diversity`[12])/ptld0010.md.er),
                              md.sgd = c(tmatrix.2000.2010.final$`Asian, moderate diversity`[8]/tmatrix.2000.2010.final$Total[8],
                                         tmatrix.2000.2010.final$`Black, moderate diversity`[9]/tmatrix.2000.2010.final$Total[9],
                                         tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[10]/tmatrix.2000.2010.final$Total[10],
                                         tmatrix.2000.2010.final$`White, moderate diversity`[12]/tmatrix.2000.2010.final$Total[12],
                                         sum(tmatrix.2000.2010.final$`aian, moderate diversity`[7] , 
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[8] , 
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[10] , 
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[11] , 
                                             tmatrix.2000.2010.final$`White, moderate diversity`[12])/ptld0010.md.er),
                              md.ogd = c(sum(tmatrix.2000.2010.final$`aian, moderate diversity`[8] , 
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[8] , 
                                             tmatrix.2000.2010.final$`White, moderate diversity`[8])/tmatrix.2000.2010.final$Total[8],
                                         sum(tmatrix.2000.2010.final$`aian, moderate diversity`[9] , 
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[9] , 
                                             tmatrix.2000.2010.final$`White, moderate diversity`[9])/tmatrix.2000.2010.final$Total[9],
                                         sum(tmatrix.2000.2010.final$`aian, moderate diversity`[10] , 
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[10] , 
                                             tmatrix.2000.2010.final$`White, moderate diversity`[10])/tmatrix.2000.2010.final$Total[10],
                                         sum(tmatrix.2000.2010.final$`aian, moderate diversity`[12] , 
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[12] ,
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[12] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[12],
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[12])/tmatrix.2000.2010.final$Total[12],
                                         sum(tmatrix.2000.2010.final$`Asian, moderate diversity`[7],
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[7] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[7] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[7] ,
                                             tmatrix.2000.2010.final$`White, moderate diversity`[7] ,
                                             tmatrix.2000.2010.final$`aian, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`White, moderate diversity`[8] ,
                                             tmatrix.2000.2010.final$`aian, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`White, moderate diversity`[9] ,
                                             tmatrix.2000.2010.final$`aian, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`White, moderate diversity`[10] ,
                                             tmatrix.2000.2010.final$`aian, moderate diversity`[11] ,
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[11] ,
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[11] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[11] ,
                                             tmatrix.2000.2010.final$`White, moderate diversity`[11] ,
                                             tmatrix.2000.2010.final$`aian, moderate diversity`[12] ,
                                             tmatrix.2000.2010.final$`Asian, moderate diversity`[12] ,
                                             tmatrix.2000.2010.final$`Black, moderate diversity`[12] ,
                                             tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[12],
                                             tmatrix.2000.2010.final$`Other, moderate diversity`[12])/ptld0010.md.er),
                              hd = c(tmatrix.2000.2010.final$`High diversity`[8]/tmatrix.2000.2010.final$Total[8],
                                     tmatrix.2000.2010.final$`High diversity`[9]/tmatrix.2000.2010.final$Total[9],
                                     tmatrix.2000.2010.final$`High diversity`[10]/tmatrix.2000.2010.final$Total[10],
                                     tmatrix.2000.2010.final$`High diversity`[12]/tmatrix.2000.2010.final$Total[12],
                                     sum(tmatrix.2000.2010.final$`High diversity`[7],
                                         tmatrix.2000.2010.final$`High diversity`[8],
                                         tmatrix.2000.2010.final$`High diversity`[9],
                                         tmatrix.2000.2010.final$`High diversity`[10],
                                         tmatrix.2000.2010.final$`High diversity`[11],
                                         tmatrix.2000.2010.final$`High diversity`[12])/ptld0010.md.er))

pt.hd.2000.2010 <- data.frame(ld.sgd = sum(tmatrix.2000.2010.final$`aian, low diversity`[13],
                                           tmatrix.2000.2010.final$`Asian, low diversity`[13],
                                           tmatrix.2000.2010.final$`Black, low diversity`[13],
                                           tmatrix.2000.2010.final$`Hispanic/Latino, low diversity`[13],
                                           tmatrix.2000.2010.final$`Other, low diversity`[13],
                                           tmatrix.2000.2010.final$`White, low diversity`[13])/tmatrix.2000.2010.final$Total[13],
                              ld.ogd = NA,
                              md.sgd = sum(tmatrix.2000.2010.final$`aian, moderate diversity`[13],
                                           tmatrix.2000.2010.final$`Asian, moderate diversity`[13],
                                           tmatrix.2000.2010.final$`Black, moderate diversity`[13],
                                           tmatrix.2000.2010.final$`Hispanic/Latino, moderate diversity`[13],
                                           tmatrix.2000.2010.final$`Other, moderate diversity`[13],
                                           tmatrix.2000.2010.final$`White, moderate diversity`[13])/tmatrix.2000.2010.final$Total[13],
                              md.ogd = NA,
                              hd = tmatrix.2000.2010.final$`High diversity`[13]/tmatrix.2000.2010.final$Total[13])

## appendix table a3 ##
pt.2000.2010 <- round(rbind(pt.ld.2000.2010,
                            pt.md.2000.2010,
                            pt.hd.2000.2010),2)

## output table ## on
stargazer(pt.2000.2010,
          title = "Probability of neighborhood transition: Metro census tracts 2000-2010",
          summary= c(rep(FALSE, 11)),
          rownames = T)


## probability of transition 2010-2020 ##

table(af.wide.final$ld_er_2010, useNA = "ifany")
table(af.wide.final$md_er_2010, useNA = "ifany")

ptld1020.ld.er <- table(af.wide.final$ld_er_2010)[2]
ptld1020.md.er <- table(af.wide.final$md_er_2010)[2]

pt.ld.2010.2020 <- data.frame(ld.sgd = c(tmatrix.2010.2020.final$`Asian, low diversity`[2]/tmatrix.2010.2020.final$Total[2],
                                         tmatrix.2010.2020.final$`Black, low diversity`[3]/tmatrix.2010.2020.final$Total[3],
                                         tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[4]/tmatrix.2010.2020.final$Total[4],
                                         tmatrix.2010.2020.final$`White, low diversity`[6]/tmatrix.2010.2020.final$Total[6],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[1] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[2] , 
                                             tmatrix.2010.2020.final$`Black, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[4] , 
                                             tmatrix.2010.2020.final$`Other, low diversity`[5] , 
                                             tmatrix.2010.2020.final$`White, low diversity`[6])/ptld1020.ld.er),
                              ld.ogd = c(sum(tmatrix.2010.2020.final$`aian, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[2])/tmatrix.2010.2020.final$Total[2],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[3])/tmatrix.2010.2020.final$Total[3],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[4])/tmatrix.2010.2020.final$Total[4],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[6] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[6] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[6] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[6],
                                             tmatrix.2010.2020.final$`Other, low diversity`[6])/tmatrix.2010.2020.final$Total[6],
                                         sum(tmatrix.2010.2020.final$`Asian, low diversity`[1] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[1] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[1] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[1] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[1] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[2] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[3] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[4] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[5] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[5] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[5] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[5] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[5] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[6] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[6] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[6] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[6],
                                             tmatrix.2010.2020.final$`Other, low diversity`[6])/ptld1020.ld.er),
                              md.sgd = c(tmatrix.2010.2020.final$`Asian, moderate diversity`[2]/tmatrix.2010.2020.final$Total[2],
                                         tmatrix.2010.2020.final$`Black, moderate diversity`[3]/tmatrix.2010.2020.final$Total[3],
                                         tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[4]/tmatrix.2010.2020.final$Total[4],
                                         tmatrix.2010.2020.final$`White, moderate diversity`[6]/tmatrix.2010.2020.final$Total[6],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[1],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[3],
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[4],
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[5],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[6])/ptld1020.ld.er),
                              md.ogd = c(sum(tmatrix.2010.2020.final$`aian, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[2])/tmatrix.2010.2020.final$Total[2],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[3],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[3] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[3] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[3],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[3])/tmatrix.2010.2020.final$Total[3],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[4],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[4] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[4] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[4],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[4])/tmatrix.2010.2020.final$Total[4],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[6],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[6] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[6] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[6],
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[6])/tmatrix.2010.2020.final$Total[6],
                                         sum(tmatrix.2010.2020.final$`Asian, moderate diversity`[1],
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[1] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[1] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[1],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[1] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[2] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[2] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[2],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[2] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[3],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[3] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[3] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[3],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[3] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[4],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[4] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[4] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[4],
                                             tmatrix.2010.2020.final$`White, moderate diversity`[4] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[5],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[5],
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[5] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[5] ,
                                             tmatrix.2010.2020.final$`White, moderate diversity`[5] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[6],
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[6] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[6] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[6],
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[6])/ptld1020.ld.er),
                              hd = c(#tmatrix.2010.2020.final$`High diversity`[1]/tmatrix.2010.2020.final$Total[1],
                                tmatrix.2010.2020.final$`High diversity`[2]/tmatrix.2010.2020.final$Total[2],
                                tmatrix.2010.2020.final$`High diversity`[3]/tmatrix.2010.2020.final$Total[3],
                                tmatrix.2010.2020.final$`High diversity`[4]/tmatrix.2010.2020.final$Total[4],
                                #tmatrix.2010.2020.final$`High diversity`[5]/tmatrix.2010.2020.final$Total[5],
                                tmatrix.2010.2020.final$`High diversity`[6]/tmatrix.2010.2020.final$Total[6],
                                sum(tmatrix.2010.2020.final$`High diversity`[1]+
                                      tmatrix.2010.2020.final$`High diversity`[2] +
                                      tmatrix.2010.2020.final$`High diversity`[3]+ 
                                      tmatrix.2010.2020.final$`High diversity`[4]+
                                      tmatrix.2010.2020.final$`High diversity`[5]+
                                      tmatrix.2010.2020.final$`High diversity`[6])/ptld1020.ld.er))

pt.md.2010.2020 <- data.frame(ld.sgd = c(tmatrix.2010.2020.final$`Asian, low diversity`[8]/tmatrix.2010.2020.final$Total[8],
                                         tmatrix.2010.2020.final$`Black, low diversity`[9]/tmatrix.2010.2020.final$Total[9],
                                         tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[10]/tmatrix.2010.2020.final$Total[10],
                                         tmatrix.2010.2020.final$`White, low diversity`[12]/tmatrix.2010.2020.final$Total[12],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[7] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[8] , 
                                             tmatrix.2010.2020.final$`Black, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[10] , 
                                             tmatrix.2010.2020.final$`Other, low diversity`[11] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[12])/ptld1020.md.er),
                              ld.ogd = c(sum(tmatrix.2010.2020.final$`aian, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[8])/tmatrix.2010.2020.final$Total[8],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[9])/tmatrix.2010.2020.final$Total[9],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[10])/tmatrix.2010.2020.final$Total[10],
                                         sum(tmatrix.2010.2020.final$`aian, low diversity`[12] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[12] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[12] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[12],
                                             tmatrix.2010.2020.final$`Other, low diversity`[12])/tmatrix.2010.2020.final$Total[12],
                                         sum(tmatrix.2010.2020.final$`Asian, low diversity`[7] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[7] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[7] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[7] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[7] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[8] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[9] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`Other, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[10] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[11] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[11] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[11] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[11] ,
                                             tmatrix.2010.2020.final$`White, low diversity`[11] ,
                                             tmatrix.2010.2020.final$`aian, low diversity`[12] ,
                                             tmatrix.2010.2020.final$`Asian, low diversity`[12] ,
                                             tmatrix.2010.2020.final$`Black, low diversity`[12] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[12],
                                             tmatrix.2010.2020.final$`Other, low diversity`[12])/ptld1020.md.er),
                              md.sgd = c(tmatrix.2010.2020.final$`Asian, moderate diversity`[8]/tmatrix.2010.2020.final$Total[8],
                                         tmatrix.2010.2020.final$`Black, moderate diversity`[9]/tmatrix.2010.2020.final$Total[9],
                                         tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[10]/tmatrix.2010.2020.final$Total[10],
                                         tmatrix.2010.2020.final$`White, moderate diversity`[12]/tmatrix.2010.2020.final$Total[12],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[7] , 
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[8] , 
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[10] , 
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[11] , 
                                             tmatrix.2010.2020.final$`White, moderate diversity`[12])/ptld1020.md.er),
                              md.ogd = c(sum(tmatrix.2010.2020.final$`aian, moderate diversity`[8] , 
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[8] , 
                                             tmatrix.2010.2020.final$`White, moderate diversity`[8])/tmatrix.2010.2020.final$Total[8],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[9] , 
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[9] , 
                                             tmatrix.2010.2020.final$`White, moderate diversity`[9])/tmatrix.2010.2020.final$Total[9],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[10] , 
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[10] , 
                                             tmatrix.2010.2020.final$`White, moderate diversity`[10])/tmatrix.2010.2020.final$Total[10],
                                         sum(tmatrix.2010.2020.final$`aian, moderate diversity`[12] , 
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[12] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[12] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[12],
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[12])/tmatrix.2010.2020.final$Total[12],
                                         sum(tmatrix.2010.2020.final$`Asian, moderate diversity`[7],
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[7] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[7] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[7] ,
                                             tmatrix.2010.2020.final$`White, moderate diversity`[7] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`White, moderate diversity`[8] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`White, moderate diversity`[9] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`White, moderate diversity`[10] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[11] ,
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[11] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[11] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[11] ,
                                             tmatrix.2010.2020.final$`White, moderate diversity`[11] ,
                                             tmatrix.2010.2020.final$`aian, moderate diversity`[12] ,
                                             tmatrix.2010.2020.final$`Asian, moderate diversity`[12] ,
                                             tmatrix.2010.2020.final$`Black, moderate diversity`[12] ,
                                             tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[12],
                                             tmatrix.2010.2020.final$`Other, moderate diversity`[12])/ptld1020.md.er),
                              hd = c(tmatrix.2010.2020.final$`High diversity`[8]/tmatrix.2010.2020.final$Total[8],
                                     tmatrix.2010.2020.final$`High diversity`[9]/tmatrix.2010.2020.final$Total[9],
                                     tmatrix.2010.2020.final$`High diversity`[10]/tmatrix.2010.2020.final$Total[10],
                                     tmatrix.2010.2020.final$`High diversity`[12]/tmatrix.2010.2020.final$Total[12],
                                     sum(tmatrix.2010.2020.final$`High diversity`[7],
                                         tmatrix.2010.2020.final$`High diversity`[8],
                                         tmatrix.2010.2020.final$`High diversity`[9],
                                         tmatrix.2010.2020.final$`High diversity`[10],
                                         tmatrix.2010.2020.final$`High diversity`[11],
                                         tmatrix.2010.2020.final$`High diversity`[12])/ptld1020.md.er))

pt.hd.2010.2020 <- data.frame(ld.sgd = sum(tmatrix.2010.2020.final$`aian, low diversity`[13],
                                           tmatrix.2010.2020.final$`Asian, low diversity`[13],
                                           tmatrix.2010.2020.final$`Black, low diversity`[13],
                                           tmatrix.2010.2020.final$`Hispanic/Latino, low diversity`[13],
                                           tmatrix.2010.2020.final$`Other, low diversity`[13],
                                           tmatrix.2010.2020.final$`White, low diversity`[13])/tmatrix.2010.2020.final$Total[13],
                              ld.ogd = NA,
                              md.sgd = sum(tmatrix.2010.2020.final$`aian, moderate diversity`[13],
                                           tmatrix.2010.2020.final$`Asian, moderate diversity`[13],
                                           tmatrix.2010.2020.final$`Black, moderate diversity`[13],
                                           tmatrix.2010.2020.final$`Hispanic/Latino, moderate diversity`[13],
                                           tmatrix.2010.2020.final$`Other, moderate diversity`[13],
                                           tmatrix.2010.2020.final$`White, moderate diversity`[13])/tmatrix.2010.2020.final$Total[13],
                              md.ogd = NA,
                              hd = tmatrix.2010.2020.final$`High diversity`[13]/tmatrix.2010.2020.final$Total[13])

## appendix table a4 ##
pt.2010.2020 <- round(rbind(pt.ld.2010.2020,
                            pt.md.2010.2020,
                            pt.hd.2010.2020),2)

## output table ## 
stargazer(pt.2010.2020,
          title = "Probability of neighborhood transition: Metro census tracts 2010-2020",
          summary= c(rep(FALSE, 11)),
          rownames = T)


##################
##################
## SES DYNAMICS ##
##################
##################

#################
## 2000 - 2010 ## 
#################

## pt 1 ## 

input.vars.2000.ses <- c("linc",
                         "minc",
                         "hinc")
                         
low.diverse.rows.ses <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_ses_2010[af.wide.final$ld_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                        levels = unique(af.wide.final$ld_ses_2010)),
                   maj.ses = factor(af.wide.final$maj_ses_2010[af.wide.final$ld_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                     levels = unique(af.wide.final$maj_ses_2010)))["1",]
  
  s1 <- c(s1.temp["linc"],
          s1.temp["minc"],
          s1.temp["hinc"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_ses_2010[af.wide.final$ld_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                        levels = unique(af.wide.final$md_ses_2010)),
                   maj.ses = factor(af.wide.final$maj_ses_2010[af.wide.final$ld_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                     levels = unique(af.wide.final$maj_ses_2010)))["1",]
  
  s2 <- c(s2.temp["linc"],
          s2.temp["minc"],
          s2.temp["hinc"])
  
  s3 <- table(factor(af.wide.final$hd_ses_2010[af.wide.final$ld_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                     levels = unique(af.wide.final$hd_ses_2010)))["1"]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:7, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt1.ses <- lapply(input.vars.2000.ses, low.diverse.rows.ses)

pt1.final.ses <- bind_rows(pt1.ses)

row.names(pt1.final.ses) <- c("LI, low diversity",
                              "MI, low diversity",
                              "HI, low diversity")

names(pt1.final.ses) <- c("LI, low diversity",
                          "MI, low diversity",
                          "HI, low diversity",
                          "LI, moderate diversity",
                          "MI, moderate diversity",
                          "HI, moderate diversity",
                          "High diversity")


## pt 2 ## 

mod.diverse.rows.ses <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_ses_2010[af.wide.final$md_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                        levels = unique(af.wide.final$ld_ses_2010)),
                   maj.ses = factor(af.wide.final$maj_ses_2010[af.wide.final$md_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                     levels = unique(af.wide.final$maj_ses_2010)))["1",]
  
  s1 <- c(s1.temp["linc"],
          s1.temp["minc"],
          s1.temp["hinc"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_ses_2010[af.wide.final$md_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                        levels = unique(af.wide.final$md_ses_2010)),
                   maj.ses = factor(af.wide.final$maj_ses_2010[af.wide.final$md_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                                     levels = unique(af.wide.final$maj_ses_2010)))["1",]
  
  s2 <- c(s2.temp["linc"],
          s2.temp["minc"],
          s2.temp["hinc"])
  
  s3 <- table(factor(af.wide.final$hd_ses_2010[af.wide.final$md_ses_2000==1 & af.wide.final$maj_ses_2000 == x],
                     levels = unique(af.wide.final$hd_ses_2010)))["1"]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:7, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt2.ses <- lapply(input.vars.2000.ses, mod.diverse.rows.ses)

pt2.final.ses <- bind_rows(pt2.ses)


row.names(pt2.final.ses) <- c("LI, moderate diversity",
                              "MI, moderate diversity",
                              "HI, moderate diversity")

names(pt2.final.ses) <- c("LI, low diversity",
                          "MI, low diversity",
                          "HI, low diversity",
                          "LI, moderate diversity",
                          "MI, moderate diversity",
                          "HI, moderate diversity",
                          "High diversity")


## pt 3 ## 

s1.3.temp.ses <- table(factor(af.wide.final$ld_ses_2010[af.wide.final$hd_ses_2000==1],
                          levels = unique(af.wide.final$ld_ses_2010)),
                   factor(af.wide.final$maj_ses_2010[af.wide.final$hd_ses_2000==1],
                          levels = unique(af.wide.final$maj_ses_2010)))["1",]

s1.3.ses <- c(s1.3.temp.ses["linc"],
              s1.3.temp.ses["minc"],
              s1.3.temp.ses["hinc"])

s2.3.temp.ses <- table(factor(af.wide.final$md_ses_2010[af.wide.final$hd_ses_2000==1],
                          levels = unique(af.wide.final$md_ses_2010)),
                   factor(af.wide.final$maj_ses_2010[af.wide.final$hd_ses_2000==1],
                          levels = unique(af.wide.final$maj_ses_2010)))["1",]

s2.3.ses <- c(s2.3.temp.ses["linc"],
              s2.3.temp.ses["minc"],
              s2.3.temp.ses["hinc"])

s3.3.ses <- table(af.wide.final$hd_ses_2010[af.wide.final$hd_ses_2000 ==1])[2]

pt3.ses <- c(s1.3.ses,s2.3.ses,s3.3.ses)

pt3.final.ses <- cbind.data.frame(split(pt3.ses, rep(1:7, times=1)),
                              stringsAsFactors=F)

row.names(pt3.final.ses) <- c("High diversity")

names(pt3.final.ses) <- c("LI, low diversity",
                          "MI, low diversity",
                          "HI, low diversity",
                          "LI, moderate diversity",
                          "MI, moderate diversity",
                          "HI, moderate diversity",
                          "High diversity")

## put it all together ## 

tmatrix.2000.2010.ses <- rbind(pt1.final.ses, 
                               pt2.final.ses,
                               pt3.final.ses)

## create total columns ## 

tmatrix.2000.2010.ses$Total <- rowSums(tmatrix.2000.2010.ses)

row.total.ses <- colSums(tmatrix.2000.2010.ses)

## final transition matrix ## 

tmatrix.2000.2010.final.ses <- rbind(tmatrix.2000.2010.ses,
                                     row.total.ses)

row.names(tmatrix.2000.2010.final.ses)[8] <- "Total"


#################
## 2010 - 2020 ## 
#################

## pt 1 ## 

input.vars.2010.ses <- c("linc",
                         "minc",
                         "hinc")

low.diverse.rows.ses <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_ses_2020[af.wide.final$ld_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                        levels = unique(af.wide.final$ld_ses_2020)),
                   maj.ses = factor(af.wide.final$maj_ses_2020[af.wide.final$ld_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                    levels = unique(af.wide.final$maj_ses_2020)))["1",]
  
  s1 <- c(s1.temp["linc"],
          s1.temp["minc"],
          s1.temp["hinc"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_ses_2020[af.wide.final$ld_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                        levels = unique(af.wide.final$md_ses_2020)),
                   maj.ses = factor(af.wide.final$maj_ses_2020[af.wide.final$ld_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                    levels = unique(af.wide.final$maj_ses_2020)))["1",]
  
  s2 <- c(s2.temp["linc"],
          s2.temp["minc"],
          s2.temp["hinc"])
  
  s3 <- table(factor(af.wide.final$hd_ses_2020[af.wide.final$ld_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                     levels = unique(af.wide.final$hd_ses_2020)))["1"]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:7, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt1.ses <- lapply(input.vars.2010.ses, low.diverse.rows.ses)

pt1.final.ses <- bind_rows(pt1.ses)

row.names(pt1.final.ses) <- c("LI, low diversity",
                              "MI, low diversity",
                              "HI, low diversity")

names(pt1.final.ses) <- c("LI, low diversity",
                          "MI, low diversity",
                          "HI, low diversity",
                          "LI, moderate diversity",
                          "MI, moderate diversity",
                          "HI, moderate diversity",
                          "High diversity")


## pt 2 ## 

mod.diverse.rows.ses <- function(x){
  
  s1.temp <- table(low.diverse = factor(af.wide.final$ld_ses_2020[af.wide.final$md_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                        levels = unique(af.wide.final$ld_ses_2020)),
                   maj.ses = factor(af.wide.final$maj_ses_2020[af.wide.final$md_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                    levels = unique(af.wide.final$maj_ses_2020)))["1",]
  
  s1 <- c(s1.temp["linc"],
          s1.temp["minc"],
          s1.temp["hinc"])
  
  s2.temp <- table(mod.diverse = factor(af.wide.final$md_ses_2020[af.wide.final$md_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                        levels = unique(af.wide.final$md_ses_2020)),
                   maj.ses = factor(af.wide.final$maj_ses_2020[af.wide.final$md_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                                    levels = unique(af.wide.final$maj_ses_2020)))["1",]
  
  s2 <- c(s2.temp["linc"],
          s2.temp["minc"],
          s2.temp["hinc"])
  
  s3 <- table(factor(af.wide.final$hd_ses_2020[af.wide.final$md_ses_2010==1 & af.wide.final$maj_ses_2010 == x],
                     levels = unique(af.wide.final$hd_ses_2020)))["1"]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:7, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt2.ses <- lapply(input.vars.2010.ses, mod.diverse.rows.ses)

pt2.final.ses <- bind_rows(pt2.ses)


row.names(pt2.final.ses) <- c("LI, moderate diversity",
                              "MI, moderate diversity",
                              "HI, moderate diversity")

names(pt2.final.ses) <- c("LI, low diversity",
                          "MI, low diversity",
                          "HI, low diversity",
                          "LI, moderate diversity",
                          "MI, moderate diversity",
                          "HI, moderate diversity",
                          "High diversity")


## pt 3 ## 

s1.3.temp.ses <- table(factor(af.wide.final$ld_ses_2020[af.wide.final$hd_ses_2010==1],
                              levels = unique(af.wide.final$ld_ses_2020)),
                       factor(af.wide.final$maj_ses_2020[af.wide.final$hd_ses_2010==1],
                              levels = unique(af.wide.final$maj_ses_2020)))["1",]

s1.3.ses <- c(s1.3.temp.ses["linc"],
              s1.3.temp.ses["minc"],
              s1.3.temp.ses["hinc"])

s2.3.temp.ses <- table(factor(af.wide.final$md_ses_2020[af.wide.final$hd_ses_2010==1],
                              levels = unique(af.wide.final$md_ses_2020)),
                       factor(af.wide.final$maj_ses_2020[af.wide.final$hd_ses_2010==1],
                              levels = unique(af.wide.final$maj_ses_2020)))["1",]

s2.3.ses <- c(s2.3.temp.ses["linc"],
              s2.3.temp.ses["minc"],
              s2.3.temp.ses["hinc"])

s3.3.ses <- table(af.wide.final$hd_ses_2020[af.wide.final$hd_ses_2010 ==1])[2]

pt3.ses <- c(s1.3.ses,s2.3.ses,s3.3.ses)

pt3.final.ses <- cbind.data.frame(split(pt3.ses, rep(1:7, times=1)),
                                  stringsAsFactors=F)

row.names(pt3.final.ses) <- c("High diversity")

names(pt3.final.ses) <- c("LI, low diversity",
                          "MI, low diversity",
                          "HI, low diversity",
                          "LI, moderate diversity",
                          "MI, moderate diversity",
                          "HI, moderate diversity",
                          "High diversity")

## put it all together ## 

tmatrix.2010.2020.ses <- rbind(pt1.final.ses, 
                               pt2.final.ses,
                               pt3.final.ses)

## create total columns ## 

tmatrix.2010.2020.ses$Total <- rowSums(tmatrix.2010.2020.ses)

row.total.ses <- colSums(tmatrix.2010.2020.ses)

## final transition matrix ## 

tmatrix.2010.2020.final.ses <- rbind(tmatrix.2010.2020.ses,
                                     row.total.ses)

row.names(tmatrix.2010.2020.final.ses)[8] <- "Total"

##################
## create graph ##
##################

t2000.ses <- tmatrix.2000.2010.final.ses$Total[1:7]
tdata2000.ses <- as.data.frame(cbind(t2000.ses, 
                                 rep(2000,7)))

t2010.ses <- t(tmatrix.2000.2010.final.ses[8,1:7])
rnames.ses <- rownames(t2010.ses)

t2010b.ses <- tmatrix.2010.2020.final.ses$Total[1:7]
tdata2010b.ses <- as.data.frame(cbind(t2010.ses, 
                                  rep(2000,7)))

t2020.ses <- t(tmatrix.2010.2020.final.ses[8,1:7])

tdata2000.ses$rnames.ses <- rnames.ses
colnames(tdata2000.ses) <- c("num","year","cat")

tdata2000.final.ses <- tdata2000.ses

tdata2000.final.ses$pct <- tdata2000.final.ses$num/sum(tdata2000.final.ses$num)
sum(tdata2000.final.ses$pct)

tdata2000.final.ses$prop <- ifelse(tdata2000.final.ses$pct >= .02, 
                                   round(tdata2000.final.ses$pct,2),"")

tdata2010.ses <- as.data.frame(cbind(t2010.ses,
                                 rep(2010,7),
                                 rnames.ses))

colnames(tdata2010.ses) <- c("num","year","cat")

tdata2010.final.ses <- tdata2010.ses %>%
  mutate(num2 = as.numeric(as.character(num))) %>%
  select(-num) %>%
  rename(num = num2)

tdata2010.final.ses$pct <- tdata2010.final.ses$num/sum(tdata2010.final.ses$num)
sum(tdata2010.final.ses$pct)

tdata2010.final.ses$prop <- ifelse(tdata2010.final.ses$pct >= .02, 
                                  round(tdata2010.final.ses$pct,2),"")

tdata2020.ses <- as.data.frame(cbind(t2020.ses,
                                 rep(2020,7),
                                 rnames.ses))

colnames(tdata2020.ses) <- c("num","year","cat")

tdata2020.final.ses <- tdata2020.ses %>%
  mutate(num2 = as.numeric(as.character(num))) %>%
  select(-num) %>%
  rename(num = num2)

tdata2020.final.ses$pct <- tdata2020.final.ses$num/sum(tdata2020.final.ses$num)
sum(tdata2020.final.ses$pct)

tdata2020.final.ses$prop <- ifelse(tdata2020.final.ses$pct >= .02, 
                                   round(tdata2020.final.ses$pct,2),"")

tdata.ses <- rbind(tdata2000.final.ses,
               tdata2010.final.ses,
               tdata2020.final.ses)

rownames(tdata.ses) <- seq(1,21,by=1)

ses.fig <- ggplot(tdata.ses, 
             aes(y=pct, x=year,
                 fill=factor(cat, levels = c("LI, low diversity",
                                             "MI, low diversity",
                                             "HI, low diversity",
                                             "LI, moderate diversity",
                                             "MI, moderate diversity",
                                             "HI, moderate diversity",
                                             "High diversity")))) +
  scale_fill_manual(values=c("lightcyan",
                             "lightblue",
                             "lightskyblue",
                             "forestgreen",
                             "darkgreen",
                             "darkolivegreen",
                             "lightcoral")) +
  geom_bar(position="fill", stat="identity") + 
  geom_text(aes(label = prop), 
            position = position_stack(vjust = 0.5), size = 3) +
  xlab("Year") + 
  ylab("Share") +
  labs(fill = "Neighborhood") + 
  labs(title = "") + 
  theme_minimal()

ses.fig

## probability of transition 2000-2010 ##

table(af.wide.final$ld_ses_2000)
table(af.wide.final$md_ses_2000)

ptld0010.ld.ses <- table(af.wide.final$ld_ses_2000)[2]
ptld0010.md.ses <- table(af.wide.final$md_ses_2000)[2]

pt.ld.2000.2010.ses <- data.frame(ld.sgd = c(tmatrix.2000.2010.final.ses$`LI, low diversity`[1]/tmatrix.2000.2010.final.ses$Total[1],
                                             tmatrix.2000.2010.final.ses$`MI, low diversity`[2]/tmatrix.2000.2010.final.ses$Total[2],
                                             tmatrix.2000.2010.final.ses$`HI, low diversity`[3]/tmatrix.2000.2010.final.ses$Total[3],
                                             sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[1] ,
                                                 tmatrix.2000.2010.final.ses$`MI, low diversity`[2] , 
                                                 tmatrix.2000.2010.final.ses$`HI, low diversity`[3],na.rm=T)/ptld0010.ld.ses),
                              ld.ogd = c(sum(tmatrix.2000.2010.final.ses$`MI, low diversity`[1] ,
                                             tmatrix.2000.2010.final.ses$`HI, low diversity`[1],na.rm=T)/tmatrix.2000.2010.final.ses$Total[1],
                                         sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[2] ,
                                             tmatrix.2000.2010.final.ses$`HI, low diversity`[2],na.rm=T)/tmatrix.2000.2010.final.ses$Total[2],
                                         sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[3] ,
                                             tmatrix.2000.2010.final.ses$`MI, low diversity`[3],na.rm=T)/tmatrix.2000.2010.final.ses$Total[3],
                                         sum(tmatrix.2000.2010.final.ses$`MI, low diversity`[1] ,
                                             tmatrix.2000.2010.final.ses$`HI, low diversity`[1] ,
                                             tmatrix.2000.2010.final.ses$`LI, low diversity`[2] ,
                                             tmatrix.2000.2010.final.ses$`HI, low diversity`[2],
                                             tmatrix.2000.2010.final.ses$`LI, low diversity`[3] ,
                                             tmatrix.2000.2010.final.ses$`MI, low diversity`[3],na.rm=T)/ptld0010.ld.ses),
                              md.sgd = c(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[1]/tmatrix.2000.2010.final.ses$Total[1],
                                         tmatrix.2000.2010.final.ses$`MI, moderate diversity`[2]/tmatrix.2000.2010.final.ses$Total[2] ,
                                         tmatrix.2000.2010.final.ses$`HI, moderate diversity`[3]/tmatrix.2000.2010.final.ses$Total[3] ,
                                         sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[1] ,
                                             tmatrix.2000.2010.final.ses$`MI, moderate diversity`[2] ,
                                             tmatrix.2000.2010.final.ses$`HI, moderate diversity`[3],na.rm=T)/ptld0010.ld.ses),
                              md.ogd = c(sum(tmatrix.2000.2010.final.ses$`MI, moderate diversity`[1] ,
                                             tmatrix.2000.2010.final.ses$`HI, moderate diversity`[1],na.rm=T)/tmatrix.2000.2010.final.ses$Total[1],
                                         sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[2] ,
                                             tmatrix.2000.2010.final.ses$`HI, moderate diversity`[2],na.rm=T)/tmatrix.2000.2010.final.ses$Total[2],
                                         sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[3] ,
                                             tmatrix.2000.2010.final.ses$`MI, moderate diversity`[3],na.rm=T)/tmatrix.2000.2010.final.ses$Total[3],
                                         sum(tmatrix.2000.2010.final.ses$`MI, moderate diversity`[1] ,
                                             tmatrix.2000.2010.final.ses$`HI, moderate diversity`[1],
                                             tmatrix.2000.2010.final.ses$`LI, moderate diversity`[2] ,
                                             tmatrix.2000.2010.final.ses$`HI, moderate diversity`[2],
                                             tmatrix.2000.2010.final.ses$`LI, moderate diversity`[3] ,
                                             tmatrix.2000.2010.final.ses$`MI, moderate diversity`[3],na.rm=T)/ptld0010.ld.ses),
                              hd = c(tmatrix.2000.2010.final.ses$`High diversity`[1]/tmatrix.2000.2010.final.ses$Total[1],
                                     tmatrix.2000.2010.final.ses$`High diversity`[2]/tmatrix.2000.2010.final.ses$Total[2],
                                     tmatrix.2000.2010.final.ses$`High diversity`[3]/tmatrix.2000.2010.final.ses$Total[3],
                                     sum(tmatrix.2000.2010.final.ses$`High diversity`[1],
                                         tmatrix.2000.2010.final.ses$`High diversity`[2] ,
                                         tmatrix.2000.2010.final.ses$`High diversity`[3],na.rm=T)/ptld0010.ld.ses))


pt.md.2000.2010.ses <- data.frame(ld.sgd = c(tmatrix.2000.2010.final.ses$`LI, low diversity`[4]/tmatrix.2000.2010.final.ses$Total[4],
                                             tmatrix.2000.2010.final.ses$`MI, low diversity`[5]/tmatrix.2000.2010.final.ses$Total[5],
                                             tmatrix.2000.2010.final.ses$`HI, low diversity`[6]/tmatrix.2000.2010.final.ses$Total[6],
                                             sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`MI, low diversity`[5] , 
                                                 tmatrix.2000.2010.final.ses$`HI, low diversity`[6],na.rm=T)/ptld0010.md.ses),
                                  ld.ogd = c(sum(tmatrix.2000.2010.final.ses$`MI, low diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`HI, low diversity`[4],na.rm=T)/tmatrix.2000.2010.final.ses$Total[4],
                                             sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[5] ,
                                                 tmatrix.2000.2010.final.ses$`HI, low diversity`[5],na.rm=T)/tmatrix.2000.2010.final.ses$Total[5],
                                             sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[6] ,
                                                 tmatrix.2000.2010.final.ses$`MI, low diversity`[6],na.rm=T)/tmatrix.2000.2010.final.ses$Total[6],
                                             sum(tmatrix.2000.2010.final.ses$`MI, low diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`HI, low diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`LI, low diversity`[5] ,
                                                 tmatrix.2000.2010.final.ses$`HI, low diversity`[5],
                                                 tmatrix.2000.2010.final.ses$`LI, low diversity`[6] ,
                                                 tmatrix.2000.2010.final.ses$`MI, low diversity`[6],na.rm=T)/ptld0010.md.ses),
                                  md.sgd = c(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[4]/tmatrix.2000.2010.final.ses$Total[4],
                                             tmatrix.2000.2010.final.ses$`MI, moderate diversity`[5]/tmatrix.2000.2010.final.ses$Total[5] ,
                                             tmatrix.2000.2010.final.ses$`HI, moderate diversity`[6]/tmatrix.2000.2010.final.ses$Total[6] ,
                                             sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`MI, moderate diversity`[5] ,
                                                 tmatrix.2000.2010.final.ses$`HI, moderate diversity`[6],na.rm=T)/ptld0010.md.ses),
                                  md.ogd = c(sum(tmatrix.2000.2010.final.ses$`MI, moderate diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`HI, moderate diversity`[4],na.rm=T)/tmatrix.2000.2010.final.ses$Total[4],
                                             sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[5] ,
                                                 tmatrix.2000.2010.final.ses$`HI, moderate diversity`[5],na.rm=T)/tmatrix.2000.2010.final.ses$Total[5],
                                             sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[6] ,
                                                 tmatrix.2000.2010.final.ses$`MI, moderate diversity`[6],na.rm=T)/tmatrix.2000.2010.final.ses$Total[6],
                                             sum(tmatrix.2000.2010.final.ses$`MI, moderate diversity`[4] ,
                                                 tmatrix.2000.2010.final.ses$`HI, moderate diversity`[4],
                                                 tmatrix.2000.2010.final.ses$`LI, moderate diversity`[5] ,
                                                 tmatrix.2000.2010.final.ses$`HI, moderate diversity`[5],
                                                 tmatrix.2000.2010.final.ses$`LI, moderate diversity`[6] ,
                                                 tmatrix.2000.2010.final.ses$`MI, moderate diversity`[6],na.rm=T)/ptld0010.md.ses),
                                  hd = c(tmatrix.2000.2010.final.ses$`High diversity`[4]/tmatrix.2000.2010.final.ses$Total[4],
                                         tmatrix.2000.2010.final.ses$`High diversity`[5]/tmatrix.2000.2010.final.ses$Total[5],
                                         tmatrix.2000.2010.final.ses$`High diversity`[6]/tmatrix.2000.2010.final.ses$Total[6],
                                         sum(tmatrix.2000.2010.final.ses$`High diversity`[4],
                                             tmatrix.2000.2010.final.ses$`High diversity`[5] ,
                                             tmatrix.2000.2010.final.ses$`High diversity`[6],na.rm=T)/ptld0010.md.ses))

pt.hd.2000.2010.ses <- data.frame(ld.sgd = sum(tmatrix.2000.2010.final.ses$`LI, low diversity`[7],
                                               tmatrix.2000.2010.final.ses$`MI, low diversity`[7],
                                               tmatrix.2000.2010.final.ses$`HI, low diversity`[7],na.rm=T)/tmatrix.2000.2010.final.ses$Total[7],
                                  ld.ogd = NA,
                                  md.sgd = sum(tmatrix.2000.2010.final.ses$`LI, moderate diversity`[7],
                                               tmatrix.2000.2010.final.ses$`MI, moderate diversity`[7],
                                               tmatrix.2000.2010.final.ses$`HI, moderate diversity`[7],na.rm=T)/tmatrix.2000.2010.final.ses$Total[7],
                                  md.ogd = NA,
                                  hd = tmatrix.2000.2010.final.ses$`High diversity`[7]/tmatrix.2000.2010.final.ses$Total[7])

## appendix table a5 ##
pt.2000.2010.ses <- round(rbind(pt.ld.2000.2010.ses,
                                pt.md.2000.2010.ses,
                                pt.hd.2000.2010.ses),2)

## output table ## on
stargazer(pt.2000.2010.ses,
          title = "Probability of neighborhood transition: Metro census tracts 2000-2010",
          summary= c(rep(FALSE, 11)),
          rownames = T)



## probability of transition 2010-2020 ##

table(af.wide.final$ld_ses_2010)
table(af.wide.final$md_ses_2010)

ptld1020.ld.ses <- table(af.wide.final$ld_ses_2010)[2]
ptld1020.md.ses <- table(af.wide.final$md_ses_2010)[2]

pt.ld.2010.2020.ses <- data.frame(ld.sgd = c(tmatrix.2010.2020.final.ses$`LI, low diversity`[1]/tmatrix.2010.2020.final.ses$Total[1],
                                             tmatrix.2010.2020.final.ses$`MI, low diversity`[2]/tmatrix.2010.2020.final.ses$Total[2],
                                             tmatrix.2010.2020.final.ses$`HI, low diversity`[3]/tmatrix.2010.2020.final.ses$Total[3],
                                             sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`MI, low diversity`[2] , 
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[3],na.rm=T)/ptld0010.ld.ses),
                                  ld.ogd = c(sum(tmatrix.2010.2020.final.ses$`MI, low diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[1],na.rm=T)/tmatrix.2010.2020.final.ses$Total[1],
                                             sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[2] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[2],na.rm=T)/tmatrix.2010.2020.final.ses$Total[2],
                                             sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[3] ,
                                                 tmatrix.2010.2020.final.ses$`MI, low diversity`[3],na.rm=T)/tmatrix.2010.2020.final.ses$Total[3],
                                             sum(tmatrix.2010.2020.final.ses$`MI, low diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`LI, low diversity`[2] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[2],
                                                 tmatrix.2010.2020.final.ses$`LI, low diversity`[3] ,
                                                 tmatrix.2010.2020.final.ses$`MI, low diversity`[3],na.rm=T)/ptld1020.ld.ses),
                                  md.sgd = c(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[1]/tmatrix.2010.2020.final.ses$Total[1],
                                             tmatrix.2010.2020.final.ses$`MI, moderate diversity`[2]/tmatrix.2010.2020.final.ses$Total[2] ,
                                             tmatrix.2010.2020.final.ses$`HI, moderate diversity`[3]/tmatrix.2010.2020.final.ses$Total[3] ,
                                             sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`MI, moderate diversity`[2] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[3],na.rm=T)/ptld1020.ld.ses),
                                  md.ogd = c(sum(tmatrix.2010.2020.final.ses$`MI, moderate diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[1],na.rm=T)/tmatrix.2010.2020.final.ses$Total[1],
                                             sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[2] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[2],na.rm=T)/tmatrix.2010.2020.final.ses$Total[2],
                                             sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[3] ,
                                                 tmatrix.2010.2020.final.ses$`MI, moderate diversity`[3],na.rm=T)/tmatrix.2010.2020.final.ses$Total[3],
                                             sum(tmatrix.2010.2020.final.ses$`MI, moderate diversity`[1] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[1],
                                                 tmatrix.2010.2020.final.ses$`LI, moderate diversity`[2] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[2],
                                                 tmatrix.2010.2020.final.ses$`LI, moderate diversity`[3] ,
                                                 tmatrix.2010.2020.final.ses$`MI, moderate diversity`[3],na.rm=T)/ptld1020.ld.ses),
                                  hd = c(tmatrix.2010.2020.final.ses$`High diversity`[1]/tmatrix.2010.2020.final.ses$Total[1],
                                         tmatrix.2010.2020.final.ses$`High diversity`[2]/tmatrix.2010.2020.final.ses$Total[2],
                                         tmatrix.2010.2020.final.ses$`High diversity`[3]/tmatrix.2010.2020.final.ses$Total[3],
                                         sum(tmatrix.2010.2020.final.ses$`High diversity`[1],
                                             tmatrix.2010.2020.final.ses$`High diversity`[2] ,
                                             tmatrix.2010.2020.final.ses$`High diversity`[3],na.rm=T)/ptld1020.ld.ses))


pt.md.2010.2020.ses <- data.frame(ld.sgd = c(tmatrix.2010.2020.final.ses$`LI, low diversity`[4]/tmatrix.2010.2020.final.ses$Total[4],
                                             tmatrix.2010.2020.final.ses$`MI, low diversity`[5]/tmatrix.2010.2020.final.ses$Total[5],
                                             tmatrix.2010.2020.final.ses$`HI, low diversity`[6]/tmatrix.2010.2020.final.ses$Total[6],
                                             sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`MI, low diversity`[5] , 
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[6],na.rm=T)/ptld1020.md.ses),
                                  ld.ogd = c(sum(tmatrix.2010.2020.final.ses$`MI, low diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[4],na.rm=T)/tmatrix.2010.2020.final.ses$Total[4],
                                             sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[5] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[5],na.rm=T)/tmatrix.2010.2020.final.ses$Total[5],
                                             sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[6] ,
                                                 tmatrix.2010.2020.final.ses$`MI, low diversity`[6],na.rm=T)/tmatrix.2010.2020.final.ses$Total[6],
                                             sum(tmatrix.2010.2020.final.ses$`MI, low diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`LI, low diversity`[5] ,
                                                 tmatrix.2010.2020.final.ses$`HI, low diversity`[5],
                                                 tmatrix.2010.2020.final.ses$`LI, low diversity`[6] ,
                                                 tmatrix.2010.2020.final.ses$`MI, low diversity`[6],na.rm=T)/ptld1020.md.ses),
                                  md.sgd = c(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[4]/tmatrix.2010.2020.final.ses$Total[4],
                                             tmatrix.2010.2020.final.ses$`MI, moderate diversity`[5]/tmatrix.2010.2020.final.ses$Total[5] ,
                                             tmatrix.2010.2020.final.ses$`HI, moderate diversity`[6]/tmatrix.2010.2020.final.ses$Total[6] ,
                                             sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`MI, moderate diversity`[5] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[6],na.rm=T)/ptld1020.md.ses),
                                  md.ogd = c(sum(tmatrix.2010.2020.final.ses$`MI, moderate diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[4],na.rm=T)/tmatrix.2010.2020.final.ses$Total[4],
                                             sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[5] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[5],na.rm=T)/tmatrix.2010.2020.final.ses$Total[5],
                                             sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[6] ,
                                                 tmatrix.2010.2020.final.ses$`MI, moderate diversity`[6],na.rm=T)/tmatrix.2010.2020.final.ses$Total[6],
                                             sum(tmatrix.2010.2020.final.ses$`MI, moderate diversity`[4] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[4],
                                                 tmatrix.2010.2020.final.ses$`LI, moderate diversity`[5] ,
                                                 tmatrix.2010.2020.final.ses$`HI, moderate diversity`[5],
                                                 tmatrix.2010.2020.final.ses$`LI, moderate diversity`[6] ,
                                                 tmatrix.2010.2020.final.ses$`MI, moderate diversity`[6],na.rm=T)/ptld1020.md.ses),
                                  hd = c(tmatrix.2010.2020.final.ses$`High diversity`[4]/tmatrix.2010.2020.final.ses$Total[4],
                                         tmatrix.2010.2020.final.ses$`High diversity`[5]/tmatrix.2010.2020.final.ses$Total[5],
                                         tmatrix.2010.2020.final.ses$`High diversity`[6]/tmatrix.2010.2020.final.ses$Total[6],
                                         sum(tmatrix.2010.2020.final.ses$`High diversity`[4],
                                             tmatrix.2010.2020.final.ses$`High diversity`[5] ,
                                             tmatrix.2010.2020.final.ses$`High diversity`[6],na.rm=T)/ptld1020.md.ses))

pt.hd.2010.2020.ses <- data.frame(ld.sgd = sum(tmatrix.2010.2020.final.ses$`LI, low diversity`[7],
                                               tmatrix.2010.2020.final.ses$`MI, low diversity`[7],
                                               tmatrix.2010.2020.final.ses$`HI, low diversity`[7],na.rm=T)/tmatrix.2010.2020.final.ses$Total[7],
                                  ld.ogd = NA,
                                  md.sgd = sum(tmatrix.2010.2020.final.ses$`LI, moderate diversity`[7],
                                               tmatrix.2010.2020.final.ses$`MI, moderate diversity`[7],
                                               tmatrix.2010.2020.final.ses$`HI, moderate diversity`[7],na.rm=T)/tmatrix.2010.2020.final.ses$Total[7],
                                  md.ogd = NA,
                                  hd = tmatrix.2010.2020.final.ses$`High diversity`[7]/tmatrix.2010.2020.final.ses$Total[7])

## appendix table a6 ##
pt.2010.2020.ses <- round(rbind(pt.ld.2010.2020.ses,
                                pt.md.2010.2020.ses,
                                pt.hd.2010.2020.ses),2)

## output table ## 
stargazer(pt.2010.2020.ses,
          title = "Probability of neighborhood transition: Metro census tracts 2010-2020",
          summary= c(rep(FALSE, 11)),
          rownames = T)


#################
## 1980 - 1990 ## 
#################

load("003_8090_trts.Rda")

all.decades.m <- stata.merge(metro.tracts.8090.fin,
                             af.wide.final, 
                             "GEOID")

## check merge ##
table(all.decades.m$merge.variable, useNA = "ifany")

## keep matches ##
all.decades <- all.decades.m %>%
  filter(merge.variable == 3) %>%
  select(-merge.variable)

## pt 1 ## 

input.vars.1980 <- c("per_asianpl_1980", 
                     "per_black_1980", 
                     "per_hl_1980", 
                     "per_other_1980", 
                     "per_white_1980")

low.diverse.rows.80 <- function(x){
  
  s1.temp <- table(low.diverse = factor(all.decades$ld_er_1990[all.decades$ld_er_1980==1 & all.decades$maj_race_1980 == x],
                                        levels = unique(all.decades$ld_er_1990)),
                   maj.race = factor(all.decades$maj_race_1990[all.decades$ld_er_1980==1 & all.decades$maj_race_1980 == x],
                                     levels = unique(all.decades$maj_race_1990)))["1",]
  
  s1 <- c(s1.temp["per_asianpi_1990"],
          s1.temp["per_black_1990"],
          s1.temp["per_hl_1990"],
          s1.temp["per_other_1990"],
          s1.temp["per_white_1990"])
  
  s2.temp <- table(mod.diverse = factor(all.decades$md_er_1990[all.decades$ld_er_1980==1 & all.decades$maj_race_1980 == x],
                                        levels = unique(all.decades$md_er_1990)),
                   maj.race = factor(all.decades$maj_race_1990[all.decades$ld_er_1980==1 & all.decades$maj_race_1980 == x],
                                     levels = unique(all.decades$maj_race_1990)))["1",]
  
  s2 <- c(s2.temp["per_asianpi_1990"],
          s2.temp["per_black_1990"],
          s2.temp["per_hl_1990"],
          s2.temp["per_other_1990"],
          s2.temp["per_white_1990"])
  
  s3 <- table(factor(all.decades$hd_er_1990[all.decades$ld_er_1980==1 & all.decades$maj_race_1980 == x],
                     levels = unique(all.decades$hd_er_1990)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:11, times=1)),
                                stringsAsFactors=F)

  
  return(pt1.final)
  
}

pt1 <- lapply(input.vars.1980, low.diverse.rows.80)

pt1.final <- bind_rows(pt1)

row.names(pt1.final) <- c("Asian, low diversity",
                          "Black, low diversity",
                          "Hispanic/Latino, low diversity",
                          "Other, low diversity",
                          "White, low diversity")

names(pt1.final) <- c("Asian, low diversity",
                      "Black, low diversity",
                      "Hispanic/Latino, low diversity",
                      "Other, low diversity",
                      "White, low diversity",
                      "Asian, moderate diversity",
                      "Black, moderate diversity",
                      "Hispanic/Latino, moderate diversity",
                      "Other, moderate diversity",
                      "White, moderate diversity",
                      "High diversity")


## pt 2 ## 

mod.diverse.rows.80 <- function(x){
  
  s1.temp <- table(low.diverse = factor(all.decades$ld_er_1990[all.decades$md_er_1980==1 & all.decades$maj_race_1980 == x],
                                        levels = unique(all.decades$ld_er_1990)),
                   maj.race = factor(all.decades$maj_race_1990[all.decades$md_er_1980==1 & all.decades$maj_race_1980 == x],
                                     levels = unique(all.decades$maj_race_1990)))["1",]
  
  s1 <- c(s1.temp["per_asianpi_1990"],
          s1.temp["per_black_1990"],
          s1.temp["per_hl_1990"],
          s1.temp["per_other_1990"],
          s1.temp["per_white_1990"])
  
  s2.temp <- table(mod.diverse = factor(all.decades$md_er_1990[all.decades$md_er_1980==1 & all.decades$maj_race_1980 == x],
                                        levels = unique(all.decades$md_er_1990)),
                   maj.race = factor(all.decades$maj_race_1990[all.decades$md_er_1980==1 & all.decades$maj_race_1980 == x],
                                     levels = unique(all.decades$maj_race_1990)))["1",]
  
  s2 <- c(s2.temp["per_asianpi_1990"],
          s2.temp["per_black_1990"],
          s2.temp["per_hl_1990"],
          s2.temp["per_other_1990"],
          s2.temp["per_white_1990"])
  
  s3 <- table(factor(all.decades$hd_er_1990[all.decades$md_er_1980==1 & all.decades$maj_race_1980 == x],
                     levels = unique(all.decades$hd_er_1990)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:11, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt2 <- lapply(input.vars.1980, mod.diverse.rows.80)

pt2.final <- bind_rows(pt2)

row.names(pt2.final) <- c("Asian, moderate diversity",
                          "Black, moderate diversity",
                          "Hispanic/Latino, moderate diversity",
                          "Other, moderate diversity",
                          "White, moderate diversity")

names(pt2.final) <- c("Asian, low diversity",
                      "Black, low diversity",
                      "Hispanic/Latino, low diversity",
                      "Other, low diversity",
                      "White, low diversity",
                      "Asian, moderate diversity",
                      "Black, moderate diversity",
                      "Hispanic/Latino, moderate diversity",
                      "Other, moderate diversity",
                      "White, moderate diversity",
                      "High diversity")

## pt 3 ## 

s1.3.temp <- table(factor(all.decades$ld_er_1990[all.decades$hd_er_1980==1],
                          levels = unique(all.decades$ld_er_1990)),
                   factor(all.decades$maj_race_1990[all.decades$hd_er_1980==1],
                          levels = unique(all.decades$maj_race_1990)))["1",]

s1.3 <- c(s1.3.temp["per_asianpi_1990"],
          s1.3.temp["per_black_1990"],
          s1.3.temp["per_hl_1990"],
          s1.3.temp["per_other_1990"],
          s1.3.temp["per_white_1990"])

s2.3.temp <- table(factor(all.decades$md_er_1990[all.decades$hd_er_1980==1],
                          levels = unique(all.decades$md_er_1990)),
                   factor(all.decades$maj_race_1990[all.decades$hd_er_1980==1],
                          levels = unique(all.decades$maj_race_1990)))["1",]

s2.3 <- c(s2.3.temp["per_asianpi_1990"],
          s2.3.temp["per_black_1990"],
          s2.3.temp["per_hl_1990"],
          s2.3.temp["per_other_1990"],
          s2.3.temp["per_white_1990"])

s3.3 <- table(all.decades$hd_er_1990[all.decades$hd_er_1980 ==1])[2]

pt3 <- c(s1.3,s2.3,s3.3)

pt3.final <- cbind.data.frame(split(pt3, rep(1:11, times=1)),
                              stringsAsFactors=F)

row.names(pt3.final) <- c("High diversity")

names(pt3.final) <- c("Asian, low diversity",
                      "Black, low diversity",
                      "Hispanic/Latino, low diversity",
                      "Other, low diversity",
                      "White, low diversity",
                      "Asian, moderate diversity",
                      "Black, moderate diversity",
                      "Hispanic/Latino, moderate diversity",
                      "Other, moderate diversity",
                      "White, moderate diversity",
                      "High diversity")

## put it all together ## 

tmatrix.1980.1990 <- rbind(pt1.final, 
                           pt2.final,
                           pt3.final)

## create total columns ## 

tmatrix.1980.1990$Total <- rowSums(tmatrix.1980.1990)

row.total <- colSums(tmatrix.1980.1990)

## final transition matrix ## 

tmatrix.1980.1990.final <- rbind(tmatrix.1980.1990,
                                 row.total)

row.names(tmatrix.1980.1990.final)[12] <- "Total"

## output table ## on
stargazer(tmatrix.1980.1990.final,
          title = "Neighborhood transition matrix in all MSAs, 1980-1990",
          summary= c(rep(FALSE, 10)),
          rownames = T)


#################
## 1990 - 2000 ##
#################

## pt 1 ## 

input.vars.1990 <- c("per_aian_1990", 
                     "per_asianpi_1990", 
                     "per_black_1990", 
                     "per_hl_1990", 
                     "per_other_1990", 
                     "per_white_1990")

low.diverse.rows.1990 <- function(x){
  
  s1.temp <- table(low.diverse = factor(all.decades$ld_er_2000[all.decades$ld_er_1990==1 & all.decades$maj_race_1990 == x],
                                        levels = unique(all.decades$ld_er_2000)),
                   maj.race = factor(all.decades$maj_race_2000[all.decades$ld_er_1990==1 & all.decades$maj_race_1990 == x],
                                     levels = unique(all.decades$maj_race_2000)))["1",]
  
  s1 <- c(s1.temp["per_aian"],
          s1.temp["per_asian"],
          s1.temp["per_black"],
          s1.temp["per_hl"],
          s1.temp["per_other"],
          s1.temp["per_white"])
  
  s2.temp <- table(mod.diverse = factor(all.decades$md_er_2000[all.decades$ld_er_1990==1 & all.decades$maj_race_1990 == x],
                                        levels = unique(all.decades$md_er_2000)),
                   maj.race = factor(all.decades$maj_race_2000[all.decades$ld_er_1990==1 & all.decades$maj_race_1990 == x],
                                     levels = unique(all.decades$maj_race_2000)))["1",]
  
  s2 <- c(s2.temp["per_aian"],
          s2.temp["per_asian"],
          s2.temp["per_black"],
          s2.temp["per_hl"],
          s2.temp["per_other"],
          s2.temp["per_white"])
  
  s3 <- table(factor(all.decades$hd_er_2000[all.decades$ld_er_1990==1 & all.decades$maj_race_1990 == x],
                     levels = unique(all.decades$hd_er_2000)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:13, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt1.1990 <- lapply(input.vars.1990, low.diverse.rows.1990)

pt1.final.1990 <- bind_rows(pt1.1990)

row.names(pt1.final.1990) <- c("AIAN, low diversity",
                               "Asian, low diversity",
                               "Black, low diversity",
                               "Hispanic/Latino, low diversity",
                               "Other, low diversity",
                               "White, low diversity")

names(pt1.final.1990) <- c("AIAN, low diversity",
                           "Asian, low diversity",
                           "Black, low diversity",
                           "Hispanic/Latino, low diversity",
                           "Other, low diversity",
                           "White, low diversity",
                           "AIAN, moderate diversity",
                           "Asian, moderate diversity",
                           "Black, moderate diversity",
                           "Hispanic/Latino, moderate diversity",
                           "Other, moderate diversity",
                           "White, moderate diversity",
                           "High diversity")

## pt 2 ## 

mod.diverse.rows.1990 <- function(x){
  
  s1.temp <- table(low.diverse = factor(all.decades$ld_er_2000[all.decades$md_er_1990==1 & all.decades$maj_race_1990 == x],
                                        levels = unique(all.decades$ld_er_2000)),
                   maj.race = factor(all.decades$maj_race_2000[all.decades$md_er_1990==1 & all.decades$maj_race_1990 == x],
                                     levels = unique(all.decades$maj_race_2000)))["1",]
  
  s1 <- c(s1.temp["per_aian"],
          s1.temp["per_asian"],
          s1.temp["per_black"],
          s1.temp["per_hl"],
          s1.temp["per_other"],
          s1.temp["per_white"])
  
  s2.temp <- table(mod.diverse = factor(all.decades$md_er_2000[all.decades$md_er_1990==1 & all.decades$maj_race_1990 == x],
                                        levels = unique(all.decades$md_er_2000)),
                   maj.race = factor(all.decades$maj_race_2000[all.decades$md_er_1990==1 & all.decades$maj_race_1990 == x],
                                     levels = unique(all.decades$maj_race_2000)))["1",]
  
  s2 <- c(s2.temp["per_aian"],
          s2.temp["per_asian"],
          s2.temp["per_black"],
          s2.temp["per_hl"],
          s2.temp["per_other"],
          s2.temp["per_white"])
  
  s3 <- table(factor(all.decades$hd_er_2000[all.decades$md_er_1990==1 & all.decades$maj_race_1990 == x],
                     levels = unique(all.decades$hd_er_2000)))[2]
  
  pt1.temp <- c(s1,s2,s3)
  
  pt1.final <- cbind.data.frame(split(pt1.temp, rep(1:13, times=1)),
                                stringsAsFactors=F)
  
  return(pt1.final)
  
}

pt2.1990 <- lapply(input.vars.1990, mod.diverse.rows.1990)

pt2.final.1990 <- bind_rows(pt2.1990)

row.names(pt2.final.1990) <- c("AIAN, moderate diversity",
                               "Asian, moderate diversity",
                               "Black, moderate diversity",
                               "Hispanic/Latino, moderate diversity",
                               "Other, moderate diversity",
                               "White, moderate diversity")

names(pt2.final.1990) <- c("AIAN, low diversity",
                           "Asian, low diversity",
                           "Black, low diversity",
                           "Hispanic/Latino, low diversity",
                           "Other, low diversity",
                           "White, low diversity",
                           "AIAN, moderate diversity",
                           "Asian, moderate diversity",
                           "Black, moderate diversity",
                           "Hispanic/Latino, moderate diversity",
                           "Other, moderate diversity",
                           "White, moderate diversity",
                           "High diversity")

## pt 3 ## 

s1.3.temp.1990 <- table(factor(all.decades$ld_er_2000[all.decades$hd_er_1990==1],
                               levels = unique(all.decades$ld_er_2000)),
                        factor(all.decades$maj_race_2000[all.decades$hd_er_1990==1],
                               levels = unique(all.decades$maj_race_2000)))["1",]

s1.3.1990 <- c(s1.3.temp.1990["per_aian"],
               s1.3.temp.1990["per_asian"],
               s1.3.temp.1990["per_black"],
               s1.3.temp.1990["per_hl"],
               s1.3.temp.1990["per_other"],
               s1.3.temp.1990["per_white"])

s2.3.temp.1990 <- table(factor(all.decades$md_er_2000[all.decades$hd_er_1990==1],
                               levels = unique(all.decades$md_er_2000)),
                        factor(all.decades$maj_race_2000[all.decades$hd_er_1990==1],
                               levels = unique(all.decades$maj_race_2000)))["1",]

s2.3.1990 <- c(s2.3.temp.1990["per_aian"],
               s2.3.temp.1990["per_asian"],
               s2.3.temp.1990["per_black"],
               s2.3.temp.1990["per_hl"],
               s2.3.temp.1990["per_other"],
               s2.3.temp.1990["per_white"])

s3.3.1990 <- table(all.decades$hd_er_2000[all.decades$hd_er_1990 ==1])[2]

pt3.1990 <- c(s1.3.1990,s2.3.1990,s3.3.1990)

pt3.final.1990 <- cbind.data.frame(split(pt3.1990, rep(1:13, times=1)),
                                   stringsAsFactors=F)

row.names(pt3.final.1990) <- c("High diversity")

names(pt3.final.1990) <- c("AIAN, low diversity",
                           "Asian, low diversity",
                           "Black, low diversity",
                           "Hispanic/Latino, low diversity",
                           "Other, low diversity",
                           "White, low diversity",
                           "AIAN, moderate diversity",
                           "Asian, moderate diversity",
                           "Black, moderate diversity",
                           "Hispanic/Latino, moderate diversity",
                           "Other, moderate diversity",
                           "White, moderate diversity",
                           "High diversity")

## put it all together ## 

tmatrix.1990.2000 <- rbind(pt1.final.1990, 
                           pt2.final.1990,
                           pt3.final.1990)

## create total columns ## 

tmatrix.1990.2000$Total <- rowSums(tmatrix.1990.2000)

row.total.1990 <- colSums(tmatrix.1990.2000)

## final transition matrix ## 

tmatrix.1990.2000.final <- rbind(tmatrix.1990.2000,
                                 row.total.1990)

row.names(tmatrix.1990.2000.final)[14] <- "Total"

## output table ## on
stargazer(tmatrix.1990.2000.final,
          title = "Neighborhood transition matrix in all MSAs, 1990-2000",
          summary= c(rep(FALSE, 10)),
          rownames = T)


##################
## create graph ##
##################

t1980 <- tmatrix.1980.1990.final$Total[1:12]
tdata1980 <- as.data.frame(cbind(t1980, 
                                 rep(1980,12)))

t1990 <- t(tmatrix.1980.1990.final[12,1:12])
rnames.80 <- rownames(t1990)

t1990b <- tmatrix.1990.2000.final$Total[1:14]
tdata1990b <- as.data.frame(cbind(t1990b, 
                                  rep(1990,14)))

t2000 <- t(tmatrix.1990.2000.final[14,1:14])

tdata1980$rnames <- rnames.80
colnames(tdata1980) <- c("num","year","cat")

tdata1980.final <- tdata1980 %>%
  filter(cat %notin% c("AIAN, low diversity",
                       "AIAN, moderate diversity",
                       "Other, low diversity",
                       "Other, moderate diversity",
                       "Total"))

tdata1980.final$pct <- tdata1980.final$num/sum(tdata1980.final$num)
sum(tdata1980.final$pct)

tdata1980.final$prop <- ifelse(tdata1980.final$pct >= .02, 
                               round(tdata1980.final$pct,2),"")

rnames.90 <- rownames(t2000)

tdata1990 <- as.data.frame(cbind(t1990b,
                                 rep(1990,14),
                                 rnames.90))

colnames(tdata1990) <- c("num","year","cat")

tdata1990.final <- tdata1990 %>%
  filter(cat %notin% c("AIAN, low diversity",
                       "AIAN, moderate diversity",
                       "Other, low diversity",
                       "Other, moderate diversity",
                       "Total")) %>%
  mutate(num2 = as.numeric(as.character(num))) %>%
  select(-num) %>%
  rename(num = num2)

tdata1990.final$pct <- tdata1990.final$num/sum(tdata1990.final$num)
sum(tdata1990.final$pct)

tdata1990.final$prop <- ifelse(tdata1990.final$pct >= .02, 
                               round(tdata1990.final$pct,2),"")

tdata2000 <- as.data.frame(cbind(t2000,
                                 rep(2000,14),
                                 rnames.90))

colnames(tdata2000) <- c("num","year","cat")

tdata2000.final <- tdata2000 %>%
  filter(cat %notin% c("AIAN, low diversity",
                       "AIAN, moderate diversity",
                       "Other, low diversity",
                       "Other, moderate diversity",
                       "Total")) %>%
  mutate(num2 = as.numeric(as.character(num))) %>%
  select(-num) %>%
  rename(num = num2)

tdata2000.final$pct <- tdata2000.final$num/sum(tdata2000.final$num)
sum(tdata2000.final$pct)

tdata2000.final$prop <- ifelse(tdata2000.final$pct >= .02, 
                               round(tdata2000.final$pct,2),"")

tdata <- rbind(tdata1980.final,
               tdata1990.final,
               tdata2000.final)

rownames(tdata) <- seq(1,27,by=1)


er.8020.fig <- ggplot(tdata, 
             aes(y=pct, x=year,
                 fill=factor(cat, levels = c("Asian, low diversity",
                                             "Asian, moderate diversity",
                                             "Black, low diversity",
                                             "Black, moderate diversity",
                                             "Hispanic/Latino, low diversity",
                                             "Hispanic/Latino, moderate diversity",
                                             "White, low diversity",
                                             "White, moderate diversity",
                                             "High diversity")))) + 
  geom_bar(position="fill", stat="identity") + 
  scale_fill_manual(values=c("seagreen3",
                             "seagreen4",
                             "royalblue3",
                             "royalblue4",
                             "orchid3",
                             "orchid4",
                             "coral2",
                             "coral3",
                             "gold2")) + 
  geom_text(aes(label = prop), 
            position = position_stack(vjust = 0.5), size = 3) +
  xlab("Year") + 
  ylab("Share") +
  labs(fill = "Neighborhood") + 
  labs(title = "")

er.8020.fig

## probability of transition 1980-1990 ##

table(all.decades$ld_er_1980, useNA = "ifany")
table(all.decades$md_er_1980, useNA = "ifany")

ptld8090.ld.er <- table(all.decades$ld_er_1980)[2]
ptld8090.md.er <- table(all.decades$md_er_1980)[2]

pt.ld.1980.1990 <- data.frame(ld.sgd = c(tmatrix.1980.1990.final$`Asian, low diversity`[1]/tmatrix.1980.1990.final$Total[1],
                                         tmatrix.1980.1990.final$`Black, low diversity`[2]/tmatrix.1980.1990.final$Total[2],
                                         tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[3]/tmatrix.1980.1990.final$Total[3],
                                         tmatrix.1980.1990.final$`White, low diversity`[5]/tmatrix.1980.1990.final$Total[5],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[1] , 
                                             tmatrix.1980.1990.final$`Black, low diversity`[2] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[3] , 
                                             tmatrix.1980.1990.final$`Other, low diversity`[4] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[5])/ptld8090.ld.er),
                              ld.ogd = c(sum(tmatrix.1980.1990.final$`Black, low diversity`[1] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[1] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[1] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[1])/tmatrix.1980.1990.final$Total[1],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[2] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[2] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[2] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[2])/tmatrix.1980.1990.final$Total[2],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[3] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[3] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[3] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[3])/tmatrix.1980.1990.final$Total[3],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[5] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[5] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[5],
                                             tmatrix.1980.1990.final$`Other, low diversity`[5])/tmatrix.1980.1990.final$Total[5],
                                         sum(tmatrix.1980.1990.final$`Black, low diversity`[1] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[1] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[1] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[1] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[2] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[2] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[2] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[2] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[3] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[3] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[3] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[3] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[4] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[4] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[4] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[4] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[5] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[5] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[5],
                                             tmatrix.1980.1990.final$`Other, low diversity`[5])/ptld8090.ld.er),
                              md.sgd = c(tmatrix.1980.1990.final$`Asian, moderate diversity`[1]/tmatrix.1980.1990.final$Total[1],
                                         tmatrix.1980.1990.final$`Black, moderate diversity`[2]/tmatrix.1980.1990.final$Total[2],
                                         tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[3]/tmatrix.1980.1990.final$Total[3],
                                         tmatrix.1980.1990.final$`White, moderate diversity`[5]/tmatrix.1980.1990.final$Total[5],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[1],
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[2],
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[3],
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[4],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[5])/ptld8090.ld.er),
                              md.ogd = c(sum(tmatrix.1980.1990.final$`Black, moderate diversity`[1],
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[1],
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[1],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[1])/tmatrix.1980.1990.final$Total[1],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[2] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[2] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[2],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[2])/tmatrix.1980.1990.final$Total[2],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[3] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[3] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[3],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[3])/tmatrix.1980.1990.final$Total[3],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[5] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[5] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[5],
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[5])/tmatrix.1980.1990.final$Total[5],
                                         sum(tmatrix.1980.1990.final$`Black, moderate diversity`[1] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[1] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[1],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[1] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[2] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[2] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[2],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[2] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[3] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[3] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[3],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[3] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[4] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[4] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[4],
                                             tmatrix.1980.1990.final$`White, moderate diversity`[4] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[5] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[5] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[5],
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[5])/ptld8090.ld.er),
                              hd = c(#tmatrix.1980.1990.final$`High diversity`[1]/tmatrix.1980.1990.final$Total[1],
                                tmatrix.1980.1990.final$`High diversity`[1]/tmatrix.1980.1990.final$Total[1],
                                tmatrix.1980.1990.final$`High diversity`[2]/tmatrix.1980.1990.final$Total[2],
                                tmatrix.1980.1990.final$`High diversity`[3]/tmatrix.1980.1990.final$Total[3],
                                #tmatrix.1980.1990.final$`High diversity`[5]/tmatrix.1980.1990.final$Total[5],
                                tmatrix.1980.1990.final$`High diversity`[5]/tmatrix.1980.1990.final$Total[5],
                                sum(tmatrix.1980.1990.final$`High diversity`[1],
                                      tmatrix.1980.1990.final$`High diversity`[2],
                                      tmatrix.1980.1990.final$`High diversity`[3],
                                      tmatrix.1980.1990.final$`High diversity`[4],
                                      tmatrix.1980.1990.final$`High diversity`[5])/ptld8090.ld.er))

pt.md.1980.1990 <- data.frame(ld.sgd = c(tmatrix.1980.1990.final$`Asian, low diversity`[6]/tmatrix.1980.1990.final$Total[6],
                                         tmatrix.1980.1990.final$`Black, low diversity`[7]/tmatrix.1980.1990.final$Total[7],
                                         tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[8]/tmatrix.1980.1990.final$Total[8],
                                         tmatrix.1980.1990.final$`White, low diversity`[10]/tmatrix.1980.1990.final$Total[10],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[6] , 
                                             tmatrix.1980.1990.final$`Black, low diversity`[7] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[8] , 
                                             tmatrix.1980.1990.final$`Other, low diversity`[9] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[10])/ptld8090.md.er),
                              ld.ogd = c(sum(tmatrix.1980.1990.final$`Black, low diversity`[6] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[6] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[6] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[6])/tmatrix.1980.1990.final$Total[6],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[7] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[7] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[7] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[7])/tmatrix.1980.1990.final$Total[7],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[8] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[8] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[8] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[8])/tmatrix.1980.1990.final$Total[8],
                                         sum(tmatrix.1980.1990.final$`Asian, low diversity`[10] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[10] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[10],
                                             tmatrix.1980.1990.final$`Other, low diversity`[10])/tmatrix.1980.1990.final$Total[10],
                                         sum(tmatrix.1980.1990.final$`Black, low diversity`[6] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[6] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[6] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[6] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[7] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[7] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[7] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[7] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[8] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[8] ,
                                             tmatrix.1980.1990.final$`Other, low diversity`[8] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[8] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[9] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[9] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[9] , 
                                             tmatrix.1980.1990.final$`White, low diversity`[9] ,
                                             tmatrix.1980.1990.final$`Asian, low diversity`[10] ,
                                             tmatrix.1980.1990.final$`Black, low diversity`[10] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[10],
                                             tmatrix.1980.1990.final$`Other, low diversity`[10])/ptld8090.md.er),
                              md.sgd = c(tmatrix.1980.1990.final$`Asian, moderate diversity`[6]/tmatrix.1980.1990.final$Total[6],
                                         tmatrix.1980.1990.final$`Black, moderate diversity`[7]/tmatrix.1980.1990.final$Total[7],
                                         tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[8]/tmatrix.1980.1990.final$Total[8],
                                         tmatrix.1980.1990.final$`White, moderate diversity`[10]/tmatrix.1980.1990.final$Total[10],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[6] , 
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[8] , 
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[9] , 
                                             tmatrix.1980.1990.final$`White, moderate diversity`[10])/ptld8090.md.er),
                              md.ogd = c(sum(tmatrix.1980.1990.final$`Black, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[6])/tmatrix.1980.1990.final$Total[6],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[7])/tmatrix.1980.1990.final$Total[7],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[8])/tmatrix.1980.1990.final$Total[8],
                                         sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[10] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[10],
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[10])/tmatrix.1980.1990.final$Total[10],
                                         sum(tmatrix.1980.1990.final$`Black, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[6] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[7] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[8] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[9] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.1980.1990.final$`White, moderate diversity`[9] ,
                                             tmatrix.1980.1990.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.1980.1990.final$`Black, moderate diversity`[10] ,
                                             tmatrix.1980.1990.final$`Other, moderate diversity`[10] ,
                                             tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[10])/ptld8090.md.er),
                              hd = c(tmatrix.1980.1990.final$`High diversity`[6]/tmatrix.1980.1990.final$Total[6],
                                     tmatrix.1980.1990.final$`High diversity`[7]/tmatrix.1980.1990.final$Total[7],
                                     tmatrix.1980.1990.final$`High diversity`[8]/tmatrix.1980.1990.final$Total[8],
                                     tmatrix.1980.1990.final$`High diversity`[10]/tmatrix.1980.1990.final$Total[10],
                                     sum(tmatrix.1980.1990.final$`High diversity`[6],
                                         tmatrix.1980.1990.final$`High diversity`[7],
                                         tmatrix.1980.1990.final$`High diversity`[8],
                                         tmatrix.1980.1990.final$`High diversity`[9],
                                         tmatrix.1980.1990.final$`High diversity`[10])/ptld8090.md.er))

pt.hd.1980.1990 <- data.frame(ld.sgd = sum(tmatrix.1980.1990.final$`Asian, low diversity`[11],
                                           tmatrix.1980.1990.final$`Black, low diversity`[11],
                                           tmatrix.1980.1990.final$`Hispanic/Latino, low diversity`[11],
                                           tmatrix.1980.1990.final$`Other, low diversity`[11],
                                           tmatrix.1980.1990.final$`White, low diversity`[11])/tmatrix.1980.1990.final$Total[11],
                              ld.ogd = NA,
                              md.sgd = sum(tmatrix.1980.1990.final$`Asian, moderate diversity`[11],
                                           tmatrix.1980.1990.final$`Black, moderate diversity`[11],
                                           tmatrix.1980.1990.final$`Hispanic/Latino, moderate diversity`[11],
                                           tmatrix.1980.1990.final$`Other, moderate diversity`[11],
                                           tmatrix.1980.1990.final$`White, moderate diversity`[11])/tmatrix.1980.1990.final$Total[11],
                              md.ogd = NA,
                              hd = tmatrix.1980.1990.final$`High diversity`[11]/tmatrix.1980.1990.final$Total[11])

## appendix table a7 ##
pt.1980.1990 <- round(rbind(pt.ld.1980.1990,
                            pt.md.1980.1990,
                            pt.hd.1980.1990),2)

## output table ## on
stargazer(pt.1980.1990,
          title = "Probability of neighborhood transition: Metro census tracts 2000-2010",
          summary= c(rep(FALSE, 11)),
          rownames = T)


## probability of transition 1990-2000 ##

table(all.decades$ld_er_1990, useNA = "ifany")
table(all.decades$md_er_1990, useNA = "ifany")

ptld9000.ld.ses <- table(all.decades$ld_er_1990)[2]
ptld9000.md.ses <- table(all.decades$md_er_1990)[2]

pt.ld.1990.2000 <- data.frame(ld.sgd = c(tmatrix.1990.2000.final$`Asian, low diversity`[2]/tmatrix.1990.2000.final$Total[2],
                                         tmatrix.1990.2000.final$`Black, low diversity`[3]/tmatrix.1990.2000.final$Total[3],
                                         tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[4]/tmatrix.1990.2000.final$Total[4],
                                         tmatrix.1990.2000.final$`White, low diversity`[6]/tmatrix.1990.2000.final$Total[6],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[1] , 
                                             tmatrix.1990.2000.final$`Asian, low diversity`[2] , 
                                             tmatrix.1990.2000.final$`Black, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[5] , 
                                             tmatrix.1990.2000.final$`White, low diversity`[6])/ptld9000.ld.ses),
                              ld.ogd = c(sum(tmatrix.1990.2000.final$`aian, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[2])/tmatrix.1990.2000.final$Total[2],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[3])/tmatrix.1990.2000.final$Total[3],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[4])/tmatrix.1990.2000.final$Total[4],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[6] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[6] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[6] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[6],
                                             tmatrix.1990.2000.final$`Other, low diversity`[6])/tmatrix.1990.2000.final$Total[6],
                                         sum(tmatrix.1990.2000.final$`Asian, low diversity`[1] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[1] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[1] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[1] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[1] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[2] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[3] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[4] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[5] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[5] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[5] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[5] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[5] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[6] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[6] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[6] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[6],
                                             tmatrix.1990.2000.final$`Other, low diversity`[6])/ptld9000.ld.ses),
                              md.sgd = c(tmatrix.1990.2000.final$`Asian, moderate diversity`[2]/tmatrix.1990.2000.final$Total[2],
                                         tmatrix.1990.2000.final$`Black, moderate diversity`[3]/tmatrix.1990.2000.final$Total[3],
                                         tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[4]/tmatrix.1990.2000.final$Total[4],
                                         tmatrix.1990.2000.final$`White, moderate diversity`[6]/tmatrix.1990.2000.final$Total[6],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[1],
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[3],
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[4],
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[5],
                                             tmatrix.1990.2000.final$`White, moderate diversity`[6])/ptld9000.ld.ses),
                              md.ogd = c(sum(tmatrix.1990.2000.final$`aian, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`White, moderate diversity`[2])/tmatrix.1990.2000.final$Total[2],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[3],
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[3],
                                             tmatrix.1990.2000.final$`White, moderate diversity`[3])/tmatrix.1990.2000.final$Total[3],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[4],
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[4],
                                             tmatrix.1990.2000.final$`White, moderate diversity`[4])/tmatrix.1990.2000.final$Total[4],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[6],
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[6] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[6] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[6],
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[6])/tmatrix.1990.2000.final$Total[6],
                                         sum(tmatrix.1990.2000.final$`Asian, moderate diversity`[1],
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[1] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[1] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[1],
                                             tmatrix.1990.2000.final$`White, moderate diversity`[1] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[2] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[2] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[2],
                                             tmatrix.1990.2000.final$`White, moderate diversity`[2] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[3] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[4] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[5] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[5] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[5] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[5] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[5] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[6] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[6] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[6] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[6],
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[6])/ptld9000.ld.ses),
                              hd = c(tmatrix.1990.2000.final$`High diversity`[2]/tmatrix.1990.2000.final$Total[2],
                                     tmatrix.1990.2000.final$`High diversity`[3]/tmatrix.1990.2000.final$Total[3],
                                     tmatrix.1990.2000.final$`High diversity`[4]/tmatrix.1990.2000.final$Total[4],
                                     tmatrix.1990.2000.final$`High diversity`[6]/tmatrix.1990.2000.final$Total[6],
                                     sum(tmatrix.1990.2000.final$`High diversity`[1]+
                                           tmatrix.1990.2000.final$`High diversity`[2]+
                                           tmatrix.1990.2000.final$`High diversity`[3] +
                                           tmatrix.1990.2000.final$`High diversity`[4]+ 
                                           tmatrix.1990.2000.final$`High diversity`[5]+
                                           tmatrix.1990.2000.final$`High diversity`[6])/ptld9000.ld.ses))

pt.md.1990.2000 <- data.frame(ld.sgd = c(tmatrix.1990.2000.final$`Asian, low diversity`[8]/tmatrix.1990.2000.final$Total[8],
                                         tmatrix.1990.2000.final$`Black, low diversity`[9]/tmatrix.1990.2000.final$Total[9],
                                         tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[10]/tmatrix.1990.2000.final$Total[10],
                                         tmatrix.1990.2000.final$`White, low diversity`[12]/tmatrix.1990.2000.final$Total[12],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[7] , 
                                             tmatrix.1990.2000.final$`Asian, low diversity`[8] , 
                                             tmatrix.1990.2000.final$`Black, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[11] , 
                                             tmatrix.1990.2000.final$`White, low diversity`[12])/ptld9000.md.ses),
                              ld.ogd = c(sum(tmatrix.1990.2000.final$`aian, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[8])/tmatrix.1990.2000.final$Total[8],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[9])/tmatrix.1990.2000.final$Total[9],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[10])/tmatrix.1990.2000.final$Total[10],
                                         sum(tmatrix.1990.2000.final$`aian, low diversity`[12] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[12] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[12] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[12],
                                             tmatrix.1990.2000.final$`Other, low diversity`[12])/tmatrix.1990.2000.final$Total[12],
                                         sum(tmatrix.1990.2000.final$`Asian, low diversity`[7] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[7] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[7] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[7] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[7] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[8] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[9] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`Other, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[10] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[11] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[11] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[11] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[11] ,
                                             tmatrix.1990.2000.final$`White, low diversity`[11] ,
                                             tmatrix.1990.2000.final$`aian, low diversity`[12] ,
                                             tmatrix.1990.2000.final$`Asian, low diversity`[12] ,
                                             tmatrix.1990.2000.final$`Black, low diversity`[12] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[12],
                                             tmatrix.1990.2000.final$`Other, low diversity`[12])/ptld9000.md.ses),
                              md.sgd = c(tmatrix.1990.2000.final$`Asian, moderate diversity`[8]/tmatrix.1990.2000.final$Total[8],
                                         tmatrix.1990.2000.final$`Black, moderate diversity`[9]/tmatrix.1990.2000.final$Total[9],
                                         tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[10]/tmatrix.1990.2000.final$Total[10],
                                         tmatrix.1990.2000.final$`White, moderate diversity`[12]/tmatrix.1990.2000.final$Total[12],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[7] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[8] , 
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[11] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[12])/ptld9000.md.ses),
                              md.ogd = c(sum(tmatrix.1990.2000.final$`aian, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[8])/tmatrix.1990.2000.final$Total[8],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[9])/tmatrix.1990.2000.final$Total[9],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[10])/tmatrix.1990.2000.final$Total[10],
                                         sum(tmatrix.1990.2000.final$`aian, moderate diversity`[12] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[12] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[12] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[12],
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[12])/tmatrix.1990.2000.final$Total[12],
                                         sum(tmatrix.1990.2000.final$`Asian, moderate diversity`[7] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[7] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[7] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[7] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[7] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[8] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[9] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[10] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[11] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[11] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[11] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[11] ,
                                             tmatrix.1990.2000.final$`White, moderate diversity`[11] ,
                                             tmatrix.1990.2000.final$`aian, moderate diversity`[12] ,
                                             tmatrix.1990.2000.final$`Asian, moderate diversity`[12] ,
                                             tmatrix.1990.2000.final$`Black, moderate diversity`[12] ,
                                             tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[12],
                                             tmatrix.1990.2000.final$`Other, moderate diversity`[12])/ptld9000.md.ses),
                              hd = c(tmatrix.1990.2000.final$`High diversity`[8]/tmatrix.1990.2000.final$Total[8],
                                     tmatrix.1990.2000.final$`High diversity`[9]/tmatrix.1990.2000.final$Total[9],
                                     tmatrix.1990.2000.final$`High diversity`[10]/tmatrix.1990.2000.final$Total[10],
                                     tmatrix.1990.2000.final$`High diversity`[12]/tmatrix.1990.2000.final$Total[12],
                                     sum(tmatrix.1990.2000.final$`High diversity`[7],
                                         tmatrix.1990.2000.final$`High diversity`[8],
                                         tmatrix.1990.2000.final$`High diversity`[9],
                                         tmatrix.1990.2000.final$`High diversity`[10],
                                         tmatrix.1990.2000.final$`High diversity`[11],
                                         tmatrix.1990.2000.final$`High diversity`[12])/ptld9000.md.ses))

pt.hd.1990.2000 <- data.frame(ld.sgd = sum(tmatrix.1990.2000.final$`aian, low diversity`[13],
                                           tmatrix.1990.2000.final$`Asian, low diversity`[13],
                                           tmatrix.1990.2000.final$`Black, low diversity`[13],
                                           tmatrix.1990.2000.final$`Hispanic/Latino, low diversity`[13],
                                           tmatrix.1990.2000.final$`Other, low diversity`[13],
                                           tmatrix.1990.2000.final$`White, low diversity`[13])/tmatrix.1990.2000.final$Total[9],
                              ld.ogd = NA,
                              md.sgd = sum(tmatrix.1990.2000.final$`aian, moderate diversity`[13],
                                           tmatrix.1990.2000.final$`Asian, moderate diversity`[13],
                                           tmatrix.1990.2000.final$`Black, moderate diversity`[13],
                                           tmatrix.1990.2000.final$`Hispanic/Latino, moderate diversity`[13],
                                           tmatrix.1990.2000.final$`Other, moderate diversity`[13],
                                           tmatrix.1990.2000.final$`White, moderate diversity`[13])/tmatrix.1990.2000.final$Total[13],
                              md.ogd = NA,
                              hd = tmatrix.1990.2000.final$`High diversity`[13]/tmatrix.1990.2000.final$Total[13])

## appendix table a8 ##
pt.1990.2000 <- round(rbind(pt.ld.1990.2000,
                            pt.md.1990.2000,
                            pt.hd.1990.2000),2)

## output table ## 
stargazer(pt.1990.2000,
          title = "Probability of neighborhood transition: Metro census tracts 2010-2020",
          summary= c(rep(FALSE, 11)),
          rownames = T)

                                      

### END OF PROGRAM ###

sink()
sink(type = "message")